0

about defining method object

why my method object doesn't output? function person3(name, age) { this.name= name; this.age = age; this.yearOfBirth = bornYear; } function bornYear() { return 2016 - this.age; } var p8=new person3("Alex",29); document.write(p8.yearOfBirth); output is: function bornYear() { return (2016 - this.age); }

14th Sep 2017, 1:11 PM
john smith
john smith - avatar
2 Antworten
+ 2
Instead of p8.yearOfBirth write p8.yearOfBirth() It should work, but I wouldn't define object methods this way
15th Sep 2017, 5:13 AM
Alex N
Alex N - avatar
+ 2
Value of yearOfBirth is function, so you should use yearOfBirth as 'yearOfBirth()'.
16th Sep 2017, 9:38 AM
Siroj Samadov
Siroj Samadov - avatar