+ 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

13th Dec 2017, 9:18 PM
Falk
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()")
14th Dec 2017, 1:35 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
Without (), I think this.yearOfBirth just aliases the bornYear() function.
13th Dec 2017, 11:04 PM
Eric Blinkidu
Eric Blinkidu - avatar
+ 2
Thank you I think i got itđŸ‘đŸ»
18th Dec 2017, 6:29 AM
Falk