So there has been much discussion on the topic of accessing private members inside of prototype methods. The thought occurred to me that the following should work:
function Test(){
var private = "Private";
this.instance = function(){
return private;
};
Test.prototype.getPrivate = function(){
return private;
};
}
var test1 = new Test();
var test2 = new Test();
console.log(test1.instance === test2.instance); // false
console.log(test1.getPrivate === test2.getPrivate); // true
Turns out it does, in fact, work. I’m concerned, however, that there might be a drawback to doing this.
So my question is: Is there a drawback?
This doesn’t work the way you probably expect, as
test1‘sgetPrivate()getstest2‘s private.so it really doesn’t matter if it is inefficient as it doesn’t work.