Created
November 13, 2015 16:39
-
-
Save noahjs/6033f04fbbf9cfffb678 to your computer and use it in GitHub Desktop.
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
/** | |
* Library stateful.js | |
*/ | |
var lib; | |
var Stateful = function(){ | |
this.string = "AAA" | |
} | |
Stateful.init = function(){ | |
this.string = "BBB"; | |
} | |
Stateful.foo = function(){ | |
return this.string; | |
} | |
module.exports = function(){ | |
if( !lib ){ | |
lib = new Stateful(); | |
} | |
return lib; | |
}; | |
/** | |
* Controller A | |
*/ | |
var lib = require('stateful.js'); | |
lib.foo(); //"AAA" | |
lib.init(); // null | |
lib.foo(); // "BBB" | |
/** | |
* Controller B | |
*/ | |
var db = require('stateful.js'); | |
lib.foo(); // "BBB" | |
/** | |
* Library stateless.js | |
*/ | |
var stateless = function(){ | |
this.string = "AAA" | |
} | |
stateless.init = function(){ | |
this.string = "BBB"; | |
} | |
stateless.foo = function(){ | |
return this.string; | |
} | |
module.exports = function(){ | |
return stateless; | |
}; | |
/** | |
* Controller A | |
*/ | |
var lib = require('stateless.js'); | |
lib.foo(); //"AAA" | |
lib.init(); // null | |
lib.foo(); // "BBB" | |
/** | |
* Controller B | |
*/ | |
var db = require('stateful.js'); | |
lib.foo(); //"AAA" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment