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());

21st Jan 2021, 2:49 PM
Gayan $
Gayan $ - avatar
1 Odpowiedź
+ 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; };
21st Jan 2021, 8:44 PM
visph
visph - avatar