Skip to content

Instantly share code, notes, and snippets.

@glauberramos
Created May 28, 2013 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glauberramos/5664893 to your computer and use it in GitHub Desktop.
Save glauberramos/5664893 to your computer and use it in GitHub Desktop.
Testing different ways of creating objects in javascript
//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