I want to add a prototype parameter to JavaScript Object witch returns a sum of other parameters.
Why does this work ?:
function Item() {
this.a = 2;
this.b = 3;
this.c = this.a + this.b;
}
let it = new Item();
console.log(it.c); //5
And this does not ???:
function Item() {
this.a = 2;
this.b = 3;
}
Item.prototype.c = this.a + this.b;
console.log(it.c); // Nan
How to access a property so I can do operations in prototypes properties ?
Or need I to always declare it in the constructor of the Object ?