This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Clazz = function(num) { | |
var priv = num; | |
this.pub = priv; | |
that = this; | |
} | |
Clazz.prototype.outsider = function() { | |
return priv; | |
} | |
c = new Clazz(6); | |
c.priv; // not defined | |
c.pub; // 6 | |
c.outsider(); // not defined |
Compare it with this one:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Clazz = function(num) { | |
priv = num; // removed the var from the definition | |
this.pub = priv; | |
that = this; | |
} | |
Clazz.prototype.outsider = function() { | |
return priv; | |
} | |
c = new Clazz(6); | |
c.priv; // not defined | |
c.pub; // 6 | |
c.outsider(); // 6!!! |
No comments:
Post a Comment