+ 1
Where is the â()â?
function person(name, age) { this.name= name; this.age = age; this.yearOfBirth = bornYear; } function bornYear() { return 2016 - this.age; } var p = new person("A", 22); document.write(p.yearOfBirth()); // Outputs 1994 Why isnât it this.yearOfBirth = bornYear(); instead of this.yearOfBirth = bornYear;? Donât I have to use the exact term when calling the function? When I use bornYear(); it doesnât work
3 Answers
+ 4
Yes (@Eric Blinkidu). The parentheses "call" the function - running / evaluating it. The name without () just stores a reference that is ready to be called.
You can ignore this (it's just here to tickle memory later):
It's done in this way so that bornYear can be added to another function (like "pet" instead of "person").
Then, "this.age" can switch seamlessly between "person" and "pet" with no code changes.
(Extra ignorable until you start playing with reusing functions: if calling throws an error, try looking up "bind()")
+ 3
Without (), I think this.yearOfBirth just aliases the bornYear() function.
+ 2
Thank you I think i got itđđ»