Created
December 4, 2012 18:13
-
-
Save benbuckman/4207067 to your computer and use it in GitHub Desktop.
Testing JS references
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
// is it possible to hold a reference to a wrapper's object, | |
// such that the reference automatically updates when the object updates? | |
// (rather than staying with the original object)? | |
var assert = require('assert'); | |
function Wrapper(){ | |
this.client = null; | |
}; | |
Wrapper.prototype.setClient = function(client){ | |
this.client = client; | |
}; | |
var wrap = new Wrapper(); | |
wrap.setClient({ | |
name: 'Original' | |
}); | |
assert.equal(wrap.client.name, 'Original', 'Initial name in client'); | |
// try to hold a persistent outside ref to wrap.client | |
// - doesn't work, it just passes thru the ref to the actual object - | |
var clientRef = wrap.client; | |
assert.equal(clientRef.name, 'Original', 'Initial name in ref'); | |
wrap.setClient({ | |
name: 'New' | |
}); | |
assert.equal(wrap.client.name, 'New', 'New name in client'); | |
// fails here: | |
assert.equal(clientRef.name, 'New', 'New name in ref'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment