0

js objects

function person(name, age) { this.name= name; this.age = age; this.yearOfBirth = bornYear; } function bornYear() { return 2016 - this.age; } in the above example in order to call the bornYear function inside the object we havent use the fuction call operator '()' i think it should be this.yearOfBirth=bornYear() plz help

20th Apr 2020, 6:22 AM
Ashutosh Auti
Ashutosh Auti - avatar
1 Resposta
+ 2
See that is because you're not calling the function there, you're initializing variable with the function. You have to call it with instance of object. For example function Person(name, age) { this.name= name; this.age = age; this.yearOfBirth = bornYear; } function bornYear() { return 2016 - this.age; } var p = new Person('rick', 35); console.log(p.yearOfBirth());
20th Apr 2020, 6:25 AM
Raj Chhatrala
Raj Chhatrala - avatar