Created
May 28, 2013 18:20
-
-
Save glauberramos/5664893 to your computer and use it in GitHub Desktop.
Testing different ways of creating objects in javascript
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
//When using "new" keywork we are bounding the "this" context object to the variable that are being returned implicit in the function. | |
//When we overrite the return function we don't need to use the "new" keyword | |
//revealing module mode, functions outside the return code | |
function test1() { | |
function x(val) { | |
return 'test' + val; | |
} | |
return { | |
x: x | |
}; | |
} | |
//first example without using this and using revealing module - mirebalais style, using api return | |
function test1(val) { | |
var api = {}; | |
api.x = 'test' + val; | |
return api; | |
} | |
var a = test1("a"); | |
var b = test1("b"); | |
console.log(a.x); | |
console.log(b.x); | |
//gap model - singleton | |
myRevealingModule = (function () { | |
function publicFunction() { | |
return "revealing"; | |
} | |
return { | |
setName: publicFunction | |
}; | |
})(); | |
console.log(myRevealingModule.setName()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment