I expected this javascript code to print "a". This javascript code prints "b".
function newA() {
this.doSomething = function() {console.log("a");}
return this;
}
function newB() {
this.doSomething = function() {console.log("b");}
return this;
}
const a = newA();
const b = newB();
a.doSomething();
test.js:7 b
It still prints "b" if the order that newA
and newB
are declared in is switched.
I don't know why this is happening and I can't phrase my problem well enough to Google an explanation.