Assertion: prototype's scope | /r/HasProperty | initialy
Facets:
Source sentences:
text: Another difference is that internally-added functions have access to private variables and functions (those declared in the constructor with var ), and prototype-based or externally-added functions do not, simply because they have the wrong scope: function Obj(initialx, initialy) { var x = initialx, y = initialy; this.getX = function() { return x; } var twoX = function() { // identical to `function twoX() { ... }` return x * 2; } this.getTwoX = function() { return twoX(); } } Obj.prototype.getY = function() { return y; // fails, even if you try `this.y` } Obj.prototype.twoY = function() { return y * 2; // fails } Obj.prototype.getTwoY = function() { return twoY(); // fails } var obj = new Obj(); // obj.y : fails, you can't access "y", it is internal // obj.twoX() : fails, you can't access "twoX", it is internal // obj.getTwoX() : works, it is "public" but has access to the twoX function