开发者

Prototypal Inheritance. Whats wrong with this simple example?

开发者 https://www.devze.com 2023-02-20 08:59 出处:网络
function a (){ this.testing = \'testing\'; } function b (){ } b.prototype = new a(); co开发者_StackOverflow中文版nsole.log(b.testing);
    function a (){
        this.testing = 'testing';
    }

    function b (){

    }

    b.prototype = new a();



    co开发者_StackOverflow中文版nsole.log(b.testing);

The console shows undefined, rather than 'testing'. What am I doing wrong?


You haven't made an instance of 'b' yet.

var bInstance = new b();
console.log(bInstance.testing);

In other words, the properties of the prototype only appear on objects of type b, not on the b() constructor function itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消