For the following code...
function ClassA() {
this.name = "ClassA";
this.sayParentName = function () {
console.log(this.name);
};
};
function ClassB() {
this.name = "ClassB";
sayName = function () {
console.log(this.name);
};
};
ClassB.prototype = new ClassA();
var test1 = new ClassB();
var test2 = new ClassB();
test1.sayParentName();
test2.sayParentName();
is there only one instance of sayParentName
from the parent ClassA
in memory? Or is a new instance in memory created for each new object of ClassB
created (each ClassB
has its own sayParentName
function that it's inherited instead of just using a single sayParentName
from the parent class)
If there is a new instance created is the only way around this to set the prototype of ClassA
to be a function (sayParentName
) for example after creating the ClassA
object
ClassA.prototype.sayParentName = function() { console.log( this.name) );