Created
July 20, 2012 19:31
-
-
Save boundsj/3152737 to your computer and use it in GitHub Desktop.
Node global, module, export exercise
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
var user1 = require('./user.js'), | |
user2 = require('./user.js'), | |
assert = require('assert'); | |
// initialize the value in user1 | |
user1.name("test"); | |
// set it to another value in user2 | |
user2.name("ohai"); | |
// user1's value has been changed | |
assert.equal(user1.name(), "ohai", "oops"); | |
// make two sep user prototypes | |
var userProto1 = new user1.User("grumpy"); | |
var userProto2 = new user1.User("bumpy"); | |
// the names of the two users are different, of course! | |
assert.notEqual(userProto1.name, userProto2.name, "oops"); |
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
var _name = ""; | |
// getter & setter frankenstein! | |
// this will essentially be a global, singleton | |
exports.name = function(n) { | |
if (n !== "" & n !== undefined) { _name = n; } | |
return _name; | |
}; | |
// this is a prototype that will maintain | |
// it's own state | |
exports.User = function(n) { | |
this.name = n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment