鸭子在 Javascript 中打字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3379529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:22:01  来源:igfitidea点击:

Duck Typing in Javascript

javascriptoop

提问by Mike Rifgin

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.

有人能给我一个 JavaScript 中 Duck Typing 继承的例子吗?我正在探索 OO javascript,我听说过鸭子类型,但看不到它在 javascript 中使用的任何示例。

采纳答案by Dagg Nabbit

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. ;)

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

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.

在使用面向对象编程语言的计算机编程中,鸭子类型是一种动态类型,其中对象的当前方法和属性集确定有效语义,而不是从特定类或特定接口实现的继承。

Wikipedia - Duck typing

维基百科 - 鸭子打字

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 Shapeinterface, if it can compute an area and a perimeter, then it is a Shape.

最简单的方法是非正式地定义契约,并简单地依赖接口每一端的开发人员知道他们在做什么。Dave Thomas 将这种方法命名为“鸭子打字”——如果它走路像鸭子,叫起来像鸭子,那么它就是一只鸭子。与我们的Shape界面类似 ,如果它可以计算面积和周长,那么它是 Shape.

JavaScript Interfaces and Duck Typing

JavaScript 接口和鸭子类型

回答by Knuckles

The rule of "Duck Typing" is

鸭子打字”的规则是

If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.

如果它看起来像一只鸭子,游泳像一只鸭子,叫起来像一只鸭子,那么它很可能是一只鸭子。

In a class-based object-oriented programming language (C++, for example) to make both objects look like a duck you must inherit their classes from a common "interface" class, so the compiler would let you call duckmethods on them. That is called a strong typing.

在基于类的面向对象编程语言(例如 C++)中,要使两个对象看起来像鸭子,您必须从公共“接口”类继承它们的类,因此编译器会让您调用duck它们的方法。这称为强类型。

Now this is how it's done in Javascript:

现在这就是它在 Javascript 中的完成方式:

var duck = {  
    appearance: "feathers",  
    quack: function duck_quack(what) {  
        print(what + " quack-quack!");  
    },  
    color: "black"  
};

var someAnimal = {  
    appearance: "feathers",  
    quack: function animal_quack(what) {  
        print(what + " whoof-whoof!");  
    },  
    eyes: "yellow"  
};  

function check(who) {  
    if ((who.appearance == "feathers") && (typeof who.quack == "function")) {  
        who.quack("I look like a duck!\n");  
        return true;  
    }  
    return false;  
}  

check(duck);  // true
check(someAnimal);  // true

See, the checkfunction check whether the passed object looks like a duck (it checks appearance and its' ability to quack). We pass two different objects to it and it will return trueon both. Besides the appearance and quacking these may be completely different things, but IN THIS PARTICULAR checkfunction they behave the same way (have a common interface), they both look like a "duck". We can call the quackmethod on both objects (and who cares what they really are).

看,check函数检查传递的对象是否看起来像一只鸭子(它检查外观和它的嘎嘎能力)。我们将两个不同的对象传递给它,它会同时返回true。除了外观和嘎嘎声,这些可能是完全不同的东西,但在这个特定的check功能中,它们的行为方式相同(具有共同的接口),它们看起来都像一只“鸭子”。我们可以quack在两个对象上调用该方法(谁在乎它们到底是什么)。

回答by ceving

An example for duck typing in JavaScript are iterables. JavaScript has actually no type called Iterable, but it has a definition for an object, which is iterable.

JavaScript 中鸭子类型的一个示例是iterables。JavaScript 实际上没有名为 的类型Iterable,但它有一个对象的定义,它是可迭代的。

In order to be iterable, an object must implement the @@iterator method

为了可迭代,对象必须实现@@iterator 方法

This is the requirement for duck typing. If an object implements a method, defined in an interface, the object can be used in places, where the interface type is accepted. For iterables this is the case in loops

这是鸭子打字的要求。如果对象实现了在接口中定义的方法,则可以在接受接口类型的地方使用该对象。对于可迭代对象,循环就是这种情况

for (let value of iterable) {
}

and arrays

和数组

[...iterable]