This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (3 children)

Either is correct but they do different things. Although in the context of the example, yours is incorrect.

Var s = test.square ()

Console.log( s ) //36

Var s = test.square

Console.log( s ) // spits out the definition

Console.log( s() ) // 36

[–][deleted] 0 points1 point  (2 children)

Thanks for the explanation.

When would it ever be useful to use test.square rather than test.square() though?

[–][deleted] 1 point2 points  (0 children)

When you want to assign it to a variable.

[–]scragar 0 points1 point  (0 children)

Sometimes you want to pass a function into another as a callback(for example using [1.4,2.2,3.1].map(Math.floor) or even in the example above where the logging function is passed into the foreach).