Created
September 14, 2011 15:04
-
-
Save Orion98MC/1216816 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(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