Skip to content

Instantly share code, notes, and snippets.

@nathanaschbacher
Created October 26, 2012 02:33
Show Gist options
  • Save nathanaschbacher/3956592 to your computer and use it in GitHub Desktop.
Save nathanaschbacher/3956592 to your computer and use it in GitHub Desktop.
Trying to get a JSON replacer to detect Ctor type...
var Foo = function Foo() {
this.id = 1;
}
var Bar = function Bar(foo) {
this.id = 2;
this.buddies = {};
}
Bar.prototype.toJSON = function() {
return this.buddies;
}
Bar.prototype.__defineSetter__('foo', function(value) {
this.buddies['foo'] = value;
});
Bar.prototype.__defineGetter__('foo', function() {
return this.buddies['foo'];
});
var f = new Foo();
var b = new Bar();
b.foo = f;
function objToId(key, value) {
console.log(value.constructor);
if(value.constructor == Foo) {
return value.id;
}
else {
return value;
}
}
console.log(JSON.stringify(b, objToId));
@nathanaschbacher
Copy link
Author

So here's the problem.... The above gist works. The issue I'm having is that the same process doesn't work when it's applied to my larger project, and it's driving me nuts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment