Skip to content

Instantly share code, notes, and snippets.

@liammclennan
Created March 29, 2011 00:41
Show Gist options
  • 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();
@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