Javascript原型继承和新关键字

时间:2015-06-28 14:00:08

标签: javascript

下面的代码片段为同一构造函数(Child)的两个实例提供了不同的输出。第一个对象(instance1)没有原型,第二个(instance2)是原型,为什么?

   parent=function(){   
        parent.prototype.method1= function() {}
        parent.prototype.property = true;
    };

    child=function() {
        parent.call(this);    
        child.prototype = new parent(); 
    };

    (function(){
        var instance1 = new child();
        console.log( instance1 );  // Empty Object

        var instance2 = new child();
        console.log( instance2 ); // Object is not empty

    }());

输出:

enter image description here

JSFiddle Link

0 个答案:

没有答案