为什么JSON.stringify没有序列化原型值? [英] Why is JSON.stringify not serializing prototype values?

查看:579
本文介绍了为什么JSON.stringify没有序列化原型值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在使用JSON解析并在Node.js和浏览器中传递Javascript并遇到这个难题。

I have been working with a fair bit of JSON parsing and passing in Javascript within Node.js and browsers recently and bumped into this conundrum.

我创建的任何对象使用构造函数,无法通过JSON.stringify完全序列化,UNLESS我单独初始化构造函数中的所有值!这意味着我的原型在设计这些类时变得毫无用处。

Any objects I created using a constructor, cannot be fully serialized fully via JSON.stringify, UNLESS I initialised all values within the constructor individually! This means my prototype becomes essentially useless in designing these classes.

有人可以解释为什么以下没有像我预期的那样序列化吗?

Can someone shed some light on why the following doesn't serialize as I expect?

var ClassA = function () { this.initialisedValue = "You can see me!" };
ClassA.prototype = { initialisedValue : "You can't see me!", uninitialisedValue : "You can't see me!" };
var a = new ClassA();
var a_string = JSON.stringify(a);

会发生什么:

a_string == {initialisedValue:你可以看到我! }

a_string == { "initialisedValue" : "You can see me!" }

我希望:

a_string == {initialisedValue:你可以看到我! ,uninitialisedValue:你看不到我! }

a_string == { "initialisedValue" : "You can see me!", "uninitialisedValue" : "You can't see me!" }

推荐答案

仅仅因为这是JSON的工作方式。来自 ES5规范

Simply because this is the way JSON works. From the ES5 spec:


设K是一个内部字符串列表,由[[Enumerable]]属性为true的值的所有自有属性的名称组成。

这是有道理的,因为JSON规范中没有机制可以保留在包含继承属性的情况下将JSON字符串解析回JavaScript对象所需的信息。在你的例子中,如何解析:

This makes sense, because there is no mechanism in the JSON specification for preserving information that would be required to parse a JSON string back into a JavaScript object if inherited properties were included. In your example, how would this parsed:


{initialisedValue:你可以看到我!,uninitialisedValue:你看不到我! }

{ "initialisedValue" : "You can see me!", "uninitialisedValue" : "You can't see me!" }

除了具有2个键值对的扁平对象之外,没有任何信息可以解析它。

There is no information to parse it into anything other than a flat object with 2 key-value pairs.

如果你考虑一下,JSON并不打算直接映射到JavaScript对象。其他语言必须能够将JSON字符串解析为名称 - 值对的简单结构。如果JSON字符串包含序列化完整JavaScript范围链所需的所有信息,则其他语言可能无法将其解析为有用的东西。用Douglas Crockford在 json.org 上的话来说:

And if you think about it, JSON is not intended to map directly to JavaScript objects. Other languages must be able to parse JSON strings into simple structures of name-value pairs. If JSON strings contained all the information necessary to serialize complete JavaScript scope chains, other languages may be less capable of parsing that into something useful. In the words of Douglas Crockford on json.org:

这些[哈希表和数组]是通用数据结构。实际上,所有现代编程语言都以某种形式支持它们。可以与编程语言互换的数据格式也基于这些结构。

These [hash tables and arrays] are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.

这篇关于为什么JSON.stringify没有序列化原型值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆