How to add function prototype property that shared by objects created with that function in JavaScript

1 Answer

0 votes
function f(a, b) {
  this.a = a;
  this.b = b;
}
const obj = new f(3, 8);

console.log(obj.c);
console.log(obj);

f.prototype.c = 46;

console.log(obj.c);
console.log(obj);


objf = new f();

console.log(Object.getPrototypeOf(objf).c); 



/*
run:

undefined

{a: 3, b: 8}

46

{a: 3, b: 8} 

46

*/

 



answered Nov 19, 2020 by avibootz
...