鸭子在Javascript中打字 [英] Duck Typing in Javascript

查看:115
本文介绍了鸭子在Javascript中打字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能给我一个关于Javascript中Duck Typing继承的例子吗?我正在探索OO javascript,我听说过鸭子打字,但是看不到它在javascript中使用的任何例子。

Could someone give me an example of Duck Typing inheritance in Javascript? I'm exploring OO javascript and I've heard about duck typing but can't see any examples of it being used in javascript.

推荐答案

第二个链接给出了js中类似鸭子的模式的示例。不是说我建议这样做,但是......好吧,你问过它。 ;)

The second link gives an example of a duck-typing-like pattern in js. Not saying I recommend doing this, but... well, you asked for it. ;)


在使用
面向对象编程语言的计算机编程中,
duck typing是一种动态
键入一个对象的当前
方法和属性组
确定有效语义,而不是从特定
类或特定$ b的实现继承的
$ b interface。

In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.

维基百科 - 鸭子打字


最简单的方法是非正式地定义
合约并简单地依赖

界面两侧的开发人员知道他们在做什么。
Dave Thomas给了这个方法
鸭子打字的名字 - 如果它像鸭子那样走路,它就像一只鸭子一样嘎嘎叫,
那么它就是一只鸭子。与我们的
Shape 界面类似,如果它可以计算
区域和周长,那么它是
形状

The simplest approach is to define the contract informally and simply rely on the developers at each side of the interface to know what they are doing. Dave Thomas has given this approach the name of "duck typing" —if it walks like a duck and it quacks like a duck, then it is a duck. Similarly with our Shape interface, if it can compute an area and a perimeter, then it is a Shape.

JavaScript界面​​和鸭子打字

这篇关于鸭子在Javascript中打字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆