Created
March 29, 2011 00:41
-
-
Save liammclennan/891625 to your computer and use it in GitHub Desktop.
JavaScript Class Template
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
var myNamespace = myNamespace || {}; | |
myNamespace.MyConstructor = function(opts) { this.options = opts; }; | |
(function () { | |
function myPrivateMethod(t) { | |
console.log("myPrivateMethod" + t.options.a); | |
} | |
myNamespace.MyConstructor.prototype = { | |
options: {}, | |
myPublicMethod: function () { | |
myPrivateMethod(this); | |
} | |
}; | |
})(); | |
var o = new myNamespace.MyConstructor({a: 1}); | |
o.myPublicMethod(); | |
var o2 = new myNamespace.MyConstructor({a: "la quinta"}); | |
o2.myPublicMethod(); |
Yes, thanks for catching that.
The weakness of this approach is that the constructor cannot set private fields.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Are the "options" and "privateField" class level variables here?
It looks like any change made by an instance to options or privateField would be reflected across all instances.