Skip to content

Instantly share code, notes, and snippets.

@Orion98MC
Created September 14, 2011 15:04
Show Gist options
  • Save Orion98MC/1216816 to your computer and use it in GitHub Desktop.
Save Orion98MC/1216816 to your computer and use it in GitHub Desktop.
(function() {
// Module scope
var global = this;
// Private methods hook
var private = {};
// Helper functions
function foo() {
return "Helper foo";
}
var Widget = function (elementId, options) {
this.scope = "Foo";
// Private methods use 'self' as 'this'
var self = this;
function foo() {
return self.scope;
}
// Public (not prototyped) method that access private method directly without using 'private'
this.bar = function() { return foo() + "Bar"; };
// export private methods to the module scope so they can be used in public (prototype) methods
private = {
foo: foo
};
Widget.instances++;
};
// "Class" properties
Widget.instances = 0;
// Public methods
Widget.prototype.fooWorking = function() { return private.foo() };
Widget.prototype.fooNotWorking = function() { return this.foo() };
// Make Widget globaly accessible
global.Widget = Widget;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment