Javascript 获取重写的getter的值
我有这样的代码Javascript 获取重写的getter的值,javascript,Javascript,我有这样的代码 function A(){} Object.defineProperties(A.prototype,{ 'propName' : { 'get' : function(){ return 'hallo'; }, 'set' : function( p ){ console.log( 'setting...' ); } } }); funct
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的原型吗?如果可以,如何调用
Grettings…是的,这是可能的
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;
}
}
});