13
将JSON字符串解析为JavaScript中的特定对象原型
我知道如何解析JSON字符串并将其转换为JavaScript对象。您可以JSON.parse()在现代浏览器(和IE9 +)中使用。 太好了,但是我怎样才能把那个JavaScript对象变成一个特定的 JavaScript对象(即具有特定的原型)呢? 例如,假设您有: function Foo() { this.a = 3; this.b = 2; this.test = function() {return this.a*this.b;}; } var fooObj = new Foo(); alert(fooObj.test() ); //Prints 6 var fooJSON = JSON.parse({"a":4, "b": 3}); //Something to convert fooJSON into a Foo Object //....... (this is what I am missing) …