2009-10-13 6 views
0

만약 내가이 :호출 기능 범위 신비

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! 

내가 _name이 정의되지하지만 밖으로 동일한 기능을 작성하는 경우 다음이 정의가 아니라 왜 다음 DERIVED 기능에 Object._extends(DERIVED, BASE)를 쓸 때?

+0

'Object._extends (this, BASE)'시도 – jantimon

+0

함수의 속성 인 prototype 속성을 설정해야하므로 'this'로 지정할 수 없습니다. DERIVED – xdevel2000

답변

0

"new"를 평가할 때 엔진은 먼저 개체를 만든 다음 해당 생성자 함수를 호출합니다. 즉, 생성자에서 호출 된 "Object._extends"는 이미 만든 개체에 영향을주지 않습니다.