Skip to content

Instantly share code, notes, and snippets.

@liammclennan
Created March 29, 2011 00:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liammclennan/891625 to your computer and use it in GitHub Desktop.
Save liammclennan/891625 to your computer and use it in GitHub Desktop.
JavaScript Class Template
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();
@mleech
Copy link

mleech commented Mar 29, 2011

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.

@liammclennan
Copy link
Author

Yes, thanks for catching that.

@liammclennan
Copy link
Author

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