鸭式辩型
- 长得像鸭子,并且能够呱呱叫的就是鸭子
- 类数组「还有类Promise…」结构很像数组,但是不是Array的实例,所以无法直接调用Array.prototype上的方法,而我们期望他可以使用这些方法的,这样就需要函数借用
函数借用
//这是一个类数组对象
let obj = {
2: 3, //1
3: 4, //2
length: 2, //4
push: Array.prototype.push //将数组的方法借用给对象
};
obj.push(1);
// obj[obj.length]=1 obj.length++
obj.push(2);
// obj[obj.length]=2 obj.length++
console.log(obj);