2012-09-15 2 views
0

이 코드는 다음과 같습니다.오버 라이드 게터 값을 얻으십시오.

function A(){} 
Object.defineProperties(A.prototype,{ 
    'propName' : { 
     'get' : function(){ 
      return 'hallo'; 
     }, 
     'set' : function(p){ 
      console.log('setting...'); 
     } 
    }  
}); 

function B(){ 
    A.call(this) 
} 
B.prototype = Object.create(new A(), { 
    'propName' : { 
     'get' : function(){ 
      //do something than call getter of parent prototype 
     }, 
     'set' : function(p){ 
      //do something than call setter of parent prototype 
     }, 
    } 
}); 

getter 또는 setter의 프로토 타입을 호출 할 수 있습니까? 그렇다면 어떻게해야합니까?

그레팅 ....

답변

1

예, 가능합니다.

B.prototype = Object.create(A.prototype, { 
    'propName': { 
    get: function() { 
     var desc = Object.getOwnPropertyDescriptor(A.prototype, 'propName'); 
     var value = desc.get.call(this); 
     return 'altered ' + value; 
    } 
    } 
});