开发者

calling function scope mystery

If I have:

Object._extends = function(Derived, Base)
{
   Derived.prototype = new Base();
   Derived.prototype.constructor = Derived;
   Derived.uber = Derived.prototype;
}


var BASE = function()
{
    this._name = "BASE"
}

var DERIVED = function()
{
    Object._extends(DERIVED, BASE)
    this._age = 3;
}
// Object._extends(DERIVED, BASE) if I write here all is ok!!!

alert(new DERIVED()._name) // undefined!

when I write Ob开发者_运维技巧ject._extends(DERIVED, BASE) into DERIVED function then _name is undefined but if I write the same function out then it is not undefined but why?


When evaluating "new", engine first creates an object and then calls its constructor function, that is, "Object._extends" called in constructor has no effect on already created object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜