【发布时间】:2014-11-15 20:04:08
【问题描述】:
我对用作 Angular.js 控制器的 ES5 getter 和 setter 非常感兴趣。目前我正在做:
var helloEC5 = function(){
//constructor
this.pants = "jeans";
};
helloEC5.prototype = {
firstName: 'Seeya',
lastName: 'Latir',
get fullName() {
console.log("get")
return this.firstName + ' ' + this.lastName;
},
set fullName (name) {
console.log('set')
var words = name.toString().split(' ');
this.firstName = words[0] || '';
this.lastName = words[1] || '';
}
};
但是有没有办法在 function() 中简洁地做到这一点?我真正想要的是(伪代码);
var helloEC5 = function() {
firstName: 'Seeya',
lastName: 'Latir',
get fullName() {
console.log("get")
return this.firstName + ' ' + this.lastName;
},
set fullName (name) {
console.log('set')
var words = name.toString().split(' ');
this.firstName = words[0] || '';
this.lastName = words[1] || '';
}
};
【问题讨论】:
-
Getter/setter in constructor 的可能重复项
标签: javascript angularjs ecmascript-5