如何用这种模式定义内联getter
How to define inline getter with this pattern?
我正试图将股权属性内联并与语法有问题。用这种模式定义内联getter属性的正确方法是什么?
Account = function() {
var Number;
var Cash;
var MTMPL;
function Account(number, cash, mtmpl) {
this.Number = number;
this.Cash = cash;
this.MTMPL = mtmpl;
};
// How to define the Equity property inline?
Account.prototype.Equity = {
get: function() {
return this.Cash + this.MTMPL;
}
}
return Account;
}();
var account = new Account("123", 100, 50);
/*
Object.defineProperties(Account.prototype, {
Equity : {
get : function() {
return this.Cash + this.MTMPL;
}
}
});*/
alert(account.Equity);
在Account
构造函数中:
Object.defineProperty(this, 'Equity', {
get: function() { return this.Cash + this.MTMPL; }
});
尽管,说实话,你在上面想做什么并不清楚。
这有什么不对?
Account = function () {
var Number;
var Cash;
var MTMPL;
function Account(number,cash,mtmpl) {
this.Number = number;
this.Cash = cash;
this.MTMPL = mtmpl;
};
Object.defineProperties(Account.prototype, {
Equity : {
get : function() {
return this.Cash + this.MTMPL;
}
}
});
return Account;
}();
var account = new Account("123",100,50);
alert(account.Equity);
相关文章:
- 查看JS对象的所有键,甚至是getter定义的键
- 使用框架/语言中的getter/setter,或者定义自己的显式getter/setters
- 为什么这个 getter 返回未定义
- 在对象上定义 getter,以便所有未定义的属性查找都返回 “”
- 使用 getter 和 setters vs 在 javascript 中定义属性
- 如何定义一个构造函数,该函数使用 getter 和 setter 创建对象
- 为ES5或ES6中对象的嵌套属性简明定义Getter和Setter
- 为AngularJS的子属性定义getter setter
- 在CoffeeScript中为我的单例类定义属性getter
- 如何为已经定义的JavaScript对象定义getter属性
- JavaScript中的自定义类数组getter
- 如何在循环中定义getter和setter
- 如何用这种模式定义内联getter
- JavaScript getter定义为数组而不是函数
- 为无父级局部变量定义 Setter/Getter:不可能
- autogenerated getter是未定义的
- Chrome不会看到通过Object.defineProperty添加getter和setter来重新定义属性
- 在对象函数中定义getter/setter
- 如何为自定义小部件定义自定义getter和setter ?- Dojo
- 为什么";setter是在没有getter的情况下定义的";JSHint错误