我试图内联移动Equity属性并且语法有问题。使用此模式定义内联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);

答案 0 :(得分:2)
在Account
构造函数中:
Object.defineProperty(this, 'Equity', {
get: function() { return this.Cash + this.MTMPL; }
});
尽管如此,说实话,目前还不清楚你上面要做什么。
答案 1 :(得分:0)
这有什么问题?
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);