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
1 Réponse
+ 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());