Long ago, in the Js kingdom, there was a king who felt that the most beautiful voice in the world was the cry of a duck, so the king summoned the minister to form a choir of 1000 ducks. The ministers searched all over the country and finally found 999 ducks, but there was still one, and the last minister found a very special chicken, which was the same as the duck, so the chicken became the last member of the choir.
So everyone defines the duck type, "if it walks like a duck, and it's called a duck, then it's a duck."
Use JS to simulate this story:
var duck = { //鸭子 duckSinging:function(){ console.log("嘎"); }};var chicken = { //鸡 duckSinging:function(){ console.log("嘎") }}var choir = []; //合唱团var joinChoir = function(animal){ //加入合唱团的方法 if(animal && typeof animal.duckSinging === "function"){ choir.push(animal); console.log("恭喜加入合唱团"); }}for(var i =0;i<999;i++){ //加入999只鸭子 joinChoir(duck);}joinChoir(chicken);console.log(choir.length); //1000 现在凑齐了1000只可以嘎嘎叫的动物,不用管它是不是鸭子
As you can see, ministers don't have to check the type of animals, just make sure they have the Ducksinging method, whether it's a cat or dog
JavaScript's tolerance of variable types as a dynamic type language gives a lot of flexibility to the actual coding. Because there is no need for type detection, we can try to invoke any method of any object without having to consider whether it was originally designed to own the method.
Using the duck type idea, we can easily implement a principle in a dynamic type language without the help of a super type: "interface-oriented programming, not implementation-oriented programming." For example, if an object has a push and pop method, and these methods provide the correct implementation, it can be used as a stack. An object can be used as an array if it has the length property, or if the attribute is accessed according to the subscript (preferably with slice and splice, etc.).
In statically typed languages, it is not an easy task to implement "interface-oriented programming", which tends to transform objects upward through abstract classes or interfaces. When the object's true type is hidden behind its superclass, these objects can be replaced with each other under "monitoring" of the type-checking system. The value of object polymorphism can be manifested only when the object can be substituted with each other.
The way we convert a pseudo-array into arrays is also duck type:
var arr = Array.prototype.slice.apply({0:1,1:2,2:3,length:3});console.log(Array.isArray(arr)); //true
Can be understood as: Pseudo-array is not "duck", but the use of dynamic language can add methods to the object of the characteristics of the pseudo-array duck properties, you can think of the pseudo-array is a duck.
In duck type, the object's valid semantics are not inherited from a particular interface or implement a particular interface, but are determined by the collection of current methods and properties.
Some duck types are used in underscore and jquery.
Reference: http://book.51cto.com/art/201505/475153.htm
Https://www.cnblogs.com/happyframework/p/3239790.html
JS design mode Duck type