3 回答

TA贡献1865条经验 获得超7个赞
Object.defineProperty()在上使用Subclass.prototype。在某些浏览器中也有__defineGetter__ 并且__defineSetter__可用,但已弃用。以您的示例为例:
Object.defineProperty(Subclass.prototype, "myProperty", {
get: function myProperty() {
// code
}
});

TA贡献1831条经验 获得超9个赞
function Unit() {
this._data; // just temp value
}
Unit.prototype = {
get accreation() {
return this._data;
},
set accreation(value) {
this._data = value
},
}
Unit.prototype.edit = function(data) {
this.accreation = data; // setting
this.out();
};
Unit.prototype.out = function() {
alert(this.accreation); // getting
};
var unit = new Unit();
unit.edit('setting and getting');
function Field() {
// children
}
Field.prototype = Object.create(Unit.prototype);
Field.prototype.add = function(data) {
this.accreation = data; // setting
this.out();
}
var field1 = new Field();
field1.add('new value for getter&setter');
var field2 = new Field();
field2.out();// because field2 object has no setting
添加回答
举报