I don't understand why the following is happening:
I have the following code:
singleton = (function() {
somePrivFunction = function() {
return new B();
}
A = function() {};
B = function() {};
C = function() {};
A.prototype.a = function() { console.log("a"); };
B.prototype = A.prototype;
B.constructor = B;
B.prototype.b = function() { console.log("b"); };
C.prototype = A.prototype;
C.constructor = C;
C.prototype.c = function() { console.log("c"); };
return {
someFunction: function() {
return somePrivFunction();
}
}
})();
When I call singleton.someFunction()
it returns me an instance of B
. However all of the following work:
singleton.someFunction().b(); // Prints "b"
singleton.someFunction().a(); // Prints "a"
singleton.someFunction().c(); // Prints "c", but why? Shouldn't it be undefined?