0
JS Function solution?
JS function solution? //function outside of constructor function and associate it with objects function Person(name,age){ this.name=name; this.age=age; } function age(){ return 2021-this.age; } var P=new Person(1998); document.write(P.age());
1 Answer
+ 2
to give to all Person instances the function:
Person.prototype.age = function() {
return new Date().getFullYear()-this.age;
};
to give to only P instance the function:
P.age = function() {
return new Date().getFullYear()-this.age;
};