原型继承。这个简单的例子怎么了?

时间:2011-03-28 03:05:49

标签: javascript inheritance prototype

    function a (){
        this.testing = 'testing';
    }

    function b (){

    }

    b.prototype = new a();



    console.log(b.testing);

控制台显示未定义,而不是“测试”。我做错了什么?

1 个答案:

答案 0 :(得分:10)

你还没有制作'b'的实例。

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

换句话说,原型的属性只出现在b类型的对象上,而不出现在b()构造函数本身上。

相关问题