Assertion: prototype's function | /r/HasAspect | access
Facets:
value: to functions
type: OTHER
value: to private variables
type: TRANSITIVE-OBJECT
value: to private data
type: TRANSITIVE-OBJECT
Source sentences:
text: There is one advantage to using the first syntax though: functions created in this manner will have access to private data:
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