Created
October 13, 2014 20:24
-
-
Save hackingbeauty/e34efaf53e7ccec60455 to your computer and use it in GitHub Desktop.
Ways of creating objects
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
| // Object literal pattern aka Singleton | |
| var Animal = { | |
| speak: function(words){ | |
| console.log(words) | |
| } | |
| } | |
| // Module Pattern | |
| var Animal = (function(){ | |
| var internalVar = 1; | |
| var getVar = function(){ | |
| return internalVar; | |
| } | |
| return { | |
| getVar: getVar | |
| } | |
| }()); | |
| // AMD Module Pattern | |
| define('Animal', function(){ | |
| var internalVar = 1; | |
| var AnimalObj = function(){ | |
| var getVar = function(){ | |
| return internalVar; | |
| } | |
| } | |
| AnimalObj.prototype.getVar = function(){ | |
| } | |
| return AnimalObj; | |
| }); | |
| var Animal = require('Animal') | |
| //CommonJS method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment