+ 2

Help with functions

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()); why I can't write : document.write(p.age()); to print the age from the function?

12th Jan 2022, 3:48 PM
Eyal R
3 Respostas
+ 1
age property is not a function. it's an attribute. So Parenthesis not need there. You can call p.age but yearOfBirth is assigned a function so you need parenthesis () to call it..
12th Jan 2022, 4:00 PM
Jayakrishna 🇮🇳
+ 1
Got it now, thanks for the explanation
12th Jan 2022, 4:10 PM
Eyal R
+ 1
You're welcome..
12th Jan 2022, 4:11 PM
Jayakrishna 🇮🇳