現在、次の構文を使用して、ゲッターとセッターでクラスを定義しています。
SomeObject = function() {
this._propertyOne = 'test';
}
SomeObject.prototype.__defineGetter__('propertyOne', function() {
return this._propertyOne;
});
SomeObject.prototype.__defineSetter__('propertyOne', function(value) {
this._propertyOne = value;
});
次に、次のようにプロパティにアクセスできます。
var o = new SomeObject();
o.propertyOne = 'test2';
console.log(o.propertyOne);
非推奨でない defineProperty コマンドまたは類似のものを使用して同じことを達成するにはどうすればよいですか?
私はこのようなことを試しました:
Object.defineProperty(SomeObject.prototype, 'propertyOne', {
get: function() {
return this._propertyOne;
}.bind(this),
set: function(value) {
this._propertyOne = value;
}.bind(this)
});
しかし、うまくいきません。