Looks like Underscore library won’t treat functions in JSON as first class citizens. Why doesn’t this fiddle work?
var a = { 'f1': function(){var s='success';} };
var b = {'foo' : 'bar'};
var c = _.extend(b, a);
alert(JSON.stringify(c));
var d = _.extend({name : 'moe'}, {age : 50});
alert(JSON.stringify(d));
Why isn’t c the right value?
d seems to have the right value if we only use strings as keys and values.
How can I get around this limitation?
cdoes have the right value:Your problem is that you’re using
JSON.stringifyto produce strings foralert, there is no representation of a function in JSON soJSON.stringify(c)leavesf1out. If you useconsole.logto view your results you’ll have better luck: http://jsfiddle.net/ambiguous/7j7hu/As an aside, you should keep in mind that using
_.extendthis way:will also modify
band that might not be your intent.