Skip to content

Commit

Permalink
add namespacing
Browse files Browse the repository at this point in the history
  • Loading branch information
angus-c committed Jul 22, 2011
1 parent 05b65be commit ee61b79
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/namespacing/1.by_direct_asssignment
@@ -0,0 +1,18 @@
var myApp = {}

myApp.id = 0;

myApp.next = function() {
return myApp.id++;
}

myApp.reset = function() {
myApp.id = 0;
}

window.console && console.log(
myApp.next(),
myApp.next(),
myApp.reset(),
myApp.next()
); //0, 1, undefined, 0
16 changes: 16 additions & 0 deletions src/namespacing/2.by_direct_asssignment_with_this
@@ -0,0 +1,16 @@
var myApp = {}

myApp.id = 0;

myApp.next = function() {
return this.id++;
}

myApp.reset = function() {
this.id = 0;
}

myApp.next(); //0
myApp.next(); //1
var getNextId = myApp.next;
getNextId(); //NaN whoops!
18 changes: 18 additions & 0 deletions src/namespacing/3.using_literal_notation
@@ -0,0 +1,18 @@
var myApp = {

id: 0,

next: function() {
return this.id++;
},

reset: function() {
this.id = 0;
}
}
window.console && console.log(
myApp.next(),
myApp.next(),
myApp.reset(),
myApp.next()
) //0, 1, undefined, 0
21 changes: 21 additions & 0 deletions src/namespacing/4.the_module_pattern
@@ -0,0 +1,21 @@
var myApp = (function() {

var id= 0;

return {
next: function() {
return id++;
},

reset: function() {
id = 0;
}
};
})();

window.console && console.log(
myApp.next(),
myApp.next(),
myApp.reset(),
myApp.next()
) //0, 1, undefined, 0
19 changes: 19 additions & 0 deletions src/namespacing/5.supply_namespace_argument
@@ -0,0 +1,19 @@
var myApp = {};
(function(context) {
var id = 0;

context.next = function() {
return id++;
};

context.reset = function() {
id = 0;
}
})(myApp);

window.console && console.log(
myApp.next(),
myApp.next(),
myApp.reset(),
myApp.next()
) //0, 1, undefined, 0
19 changes: 19 additions & 0 deletions src/namespacing/6.supply_global_argument
@@ -0,0 +1,19 @@
var myApp = {};
(function(context) {
var id = 0;

context.next = function() {
return id++;
};

context.reset = function() {
id = 0;
}
})(this);

window.console && console.log(
next(),
next(),
reset(),
next()
) //0, 1, undefined, 0
19 changes: 19 additions & 0 deletions src/namespacing/7.this_as_namespace_proxy
@@ -0,0 +1,19 @@
var myApp = {};
(function() {
var id = 0;

this.next = function() {
return id++;
};

this.reset = function() {
id = 0;
}
}).apply(myApp);

window.console && console.log(
myApp.next(),
myApp.next(),
myApp.reset(),
myApp.next()
); //0, 1, undefined, 0

0 comments on commit ee61b79

Please sign in to comment.