2013-02-01 4 views
0

을 확장합니다. 나는 run 메서드를 확장하고 싶습니다. 로그 run 1뿐 아니라 run 2도 기록하십시오. 몇 가지 다른 접근 방식을 시도했지만 작동하지 않습니다.는 지금과 같은 프로토 타입을 가지고 프로토 타입 방법

A.prototype.run = function() { 
    this.run.call(this); 
    console.log('run 2'); 
} 

또는 이것에 대한 해결책을 가지고

A.prototype.run = function() { 
    arguments.callee.call(this); 
    console.log('run 2'); 
} 

누구? 차라리 run 메서드 안의 내용을 복사하지 않을 것입니다. 감사!

답변

1
A.prototype._run = A.prototype.run; 
A.prototype.run = function() { 
    this._run.call(this); 
    console.log('run 2'); 
} 
+0

감사합니다! 빠르고 쉽게! –

1

run 메서드를 재정의하여 해당 참조를 절약 할 수 있습니다.

(function (orig) { 

    A.prototype.run = function() { 
     orig.apply(this, arguments); 

     console.log('run 2'); 
    } 

}(A.prototype.run)); 

이 첫 번째 시도와 비슷하지만 run의 첫 번째 값을 유지, 그래서 당신이 시도로 효과적으로 this.run.call(this) 할 수 있습니다.