开发者

calling function scope mystery

开发者 https://www.devze.com 2022-12-08 15:25 出处:网络
If I have: Object._extends = function(Derived, Base) { Derived.prototype = new Base(); Derived.prototype.constructor = Derived;

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

精彩评论

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

关注公众号