Why and when we should use arugment.length in ember.js
Below code is taken from here. But I do not understand, why at the time
that the author use fullName as computed property, he did the check for
setter with argument.length instead of value.length, which may more
related to the value variable that is assigned to the function. I wonder
what is the difference and why he is using arugment.length in this case?
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
No comments:
Post a Comment