Skip to content

原型、原型链、继承、公用属性和方法(.__proto__ | .prototype) #20

@yongheng2016

Description

@yongheng2016

继承

① 使用一个方法或函数,首先从自身找,如果没有,找原型: .__proto__ .__proto__.__proto__...依次

找下去

② 如果我要用的方法本身包括原型都没有,可以借啊

这个有一个类数组对象:arr 我想用Attray的一些方法,怎么办?

[].concat.call(arr, [1,2,3])  //空数组通过原型链可以找到这个方法,我arr通过call调用就好了

Array.prototype.concat.call(arr, [1,2,3])  //Array之久就有这个方法,我arr通过call调用就好了

arr.concat([1,2,3])  //如果arr是数组,上面的形式就像这样

具体示例

    var arr = [4,5,6]

    //下面这三个函数是等价的

    var arr1 = [].concat.call(arr, [1,2,3])  
    //arr --> [].call --> concat方法

    var arr2 = Array.prototype.concat.call(arr, [1,2,3])  
    //arr --> Array.prototype.call --> concat方法

    var arr3 = arr.concat([1,2,3])  

    console.log(arr1)  //[4,5,6,1,2,3]
    console.log(arr2)  //[4,5,6,1,2,3]
    console.log(arr3)  //[4,5,6,1,2,3]

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions