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
        },
    }
});

ゲッターまたはセッターのプロトタイプを呼び出すことは可能ですか?はいの場合、どのように行いますか?

グレッティング....

4

1 に答える 1

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;
    }
  }
});
于 2012-09-15T10:41:20.340 に答える