开发者

How inherits from lazy instantiation object?

In my开发者_Python百科 JavaScript code I have a lazy instantiation object AAA. I want make new object BBB and inherits from AAA.

How it's make?


standard javascript inheritance may be achieved with prototypes

BBB.prototype = new AAA();

But in this way you can not access parent methods if they are overridden. For this reason I'm using static property superclass e.g.

function AAA() {}
AAA.prototype = {
foo: function() {return 'foo';}
}

function BBB() {}
BBB.superclass = AAA.prototype;
BBB.prototype = {
foo: function() { return BBB.superclass.foo() + "bar";}
}
b = new BBB();
b.foo() //returns foobar


I like to cheat, this will create objects inheriting from a common o. I took the liberty of renaming variables you asked in your question.

var o = {
    happy: true,
    fun: true,
    time: true
}

var AAA = {
    happy: false
};
AAA.__proto__ = o;

console.log( 'happy', AAA.happy );
console.log( 'fun', AAA.fun );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜