Created
July 27, 2012 05:17
-
-
Save mhulse/3186282 to your computer and use it in GitHub Desktop.
Javascript: IIFE example...
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 GO = (function(go) { | |
go = go || {}; | |
go.init = function(x) { | |
console.log(x); | |
}; | |
go.hello = function(msg) { | |
console.log('Hello: ' + msg); | |
}; | |
return go; | |
}(GO || {})); | |
GO.init('blah'); | |
GO.hello('Billy'); |
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 GO = (function(go) { | |
// We've already determined if GO is defined, so no need to do the same thing here, right? | |
go.init = function(x) { | |
console.log(x); | |
}; | |
go.hello = function(msg) { | |
console.log('Hello: ' + msg); | |
}; | |
return go; | |
}(GO || {})); // Pass GO if defined, otherwise pass empty object. |
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 GO = (function(go) { | |
go = go || {}; // Do our check here. | |
go.init = function(x) { | |
console.log(x); | |
}; | |
go.hello = function(msg) { | |
console.log('Hello: ' + msg); | |
}; | |
return go; | |
}(GO)); // Cleaner! Thanks @preaction! :) | |
GO.init('blah'); | |
GO.hello('Billy'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An awesome singleton example can be found here.