Skip to content

Instantly share code, notes, and snippets.

@benbuckman
Created December 4, 2012 18:13
Show Gist options
  • Save benbuckman/4207067 to your computer and use it in GitHub Desktop.
Save benbuckman/4207067 to your computer and use it in GitHub Desktop.
Testing JS references
// 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