Created
October 26, 2012 02:33
-
-
Save nathanaschbacher/3956592 to your computer and use it in GitHub Desktop.
Trying to get a JSON replacer to detect Ctor type...
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 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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.