Reputation: 47
I am very new to Javascript and have looked at someone elses code to try and work out what it does. I have seen the following, and can't understand why 'total' has been set twice using different notation. Am I being really dense here? The code is:
contents = {
apples : 0,
oranges : 0,
bananas : 0,
grapes : 0,
peach : 0,
total : function() {return this.apples+this.oranges+this.bananas+this.grapes+this.peach;}
};
contents.total = function() {return this.apples+this.oranges+this.bananas+this.grapes+this.peach;};
I know that contents.total is outside of making the object, but why set the property immediately after if it's already set to the same thing?
Upvotes: 0
Views: 49
Reputation: 515
If these statements are immediately subsequent, there is no obvious reason to re-assign the same value to the total attribute. However, if the statements are interleaved by others, it may be that the instruction is updating the contents of the total attribute.
Upvotes: 1