开发者

Generate JSON to Convert into ECMAScript 5 Ready Consumption

In my client/server web app, I am generating large JSON strings (using JSON.NET JToken classes in C# web services) which I pass via AJAX Get requests to the client.

I would like to be able to define certain properties in the ES5 style: prop: { value: '1', enumerable: false, writable: true }, but there's a hitch.

First, JSON.parse() simply interprets the 'enumerable' and 'writable' properties as generic properties on a generic JavaScript object. This is probably expected and desirable behavior, but is the a comparable method to 'parse' the JSON with respect to new ES5 property attributes?

Second, Object.create() works, but it has some limitations. For example,

//Native works
var prop = Object.create(Object.prototype, { prop: { value: '1', enumerable: false, writable: true } });
//prop === "1"

//Parse works
var prop = Object.create(Object.prototype, JSON.parse({ "prop": { "value": '1', "enumerable": false, "writable": t开发者_JS百科rue } });
//prop === "1"    

//Parse fails
var prop = Object.create(Object.prototype, JSON.parse({ "prop": { "value": '1', "enumerable": "false", "writable": "true" } });
// prop === JSON.parse({ "prop": { "value": '1', "enumerable": "false", "writable": "true" } })

The problem here, of course, is that "true" !== true, which instructs Object.create() to return the same object as JSON.parse(). Further, either because of the truthy evaluation of a particular prop attribute OR because I'm attempting to create particularly large objects, Object.create() seems to be quite fragile.

Is there a better way to "parse" these objects in a way that respects the ES5 prop attributes?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜