【问题标题】:How does the prototype chain work?原型链是如何工作的?
【发布时间】:2014-11-30 18:02:53
【问题描述】:
var A = {};
var B = Object.create(A);
var C = Object.create(B);
A.isPrototypeOf(C);//returns true
C.isPrototypeOf(A);//returns false

在上面的代码中,我不明白C.isPrototypeOf(A);中结果为假的原因

【问题讨论】:

    标签: javascript


    【解决方案1】:
    var A = {}; // A inherits Object
    var B = Object.create(A); // B inherits A inherits Object
    var C = Object.create(B); // C inherits B inherits A inherits Object
    
    // Does C inherit A?
    A.isPrototypeOf(C); // true, yes
    // C inherits A because    B inherits A    and    C inherits B
    
    // Does A inherit C?
    C.isPrototypeOf(A); // false, no
    // A only inherits Object
    

    【讨论】:

      【解决方案2】:

      CB 的“后代”,它是 A 的“后代”。 C怎么可能是A的原型?

      由于C最终继承自A,而AC中得不到任何东西,那么“CA的原型”是错误的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多