下面的代码取自这里。但我不明白,为什么在作者使用 fullName 作为计算属性的时候,他用arguments.length
而不是检查 setter value.length
,这可能与value
分配给函数的变量有关。我想知道有什么区别以及为什么他在这种情况下使用 arguments.length?
App.Person = Ember.Object.extend({
firstName: null,
lastName: null,
fullName: function(key, value) {
// setter
if (arguments.length > 1) {
var nameParts = value.split(/\s+/);
this.set('firstName', nameParts[0]);
this.set('lastName', nameParts[1]);
}
// getter
return this.get('firstName') + ' ' + this.get('lastName');
}.property('firstName', 'lastName')
});
var captainAmerica = App.Person.create();
captainAmerica.set('fullName', "William Burnside");
captainAmerica.get('firstName'); // William
captainAmerica.get('lastName'); // Burnside