0

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?

2
  • 2
    No valid reason. Commented Feb 26, 2019 at 14:19
  • 3
    No, you're not being dense. This code is weird, one of the two lines is indeed superfluous. Commented Feb 26, 2019 at 14:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.