的toJSON()和JSON.Stringify之间差() [英] Difference between toJSON() and JSON.Stringify()
问题描述
如果您需要读取或克隆所有模型的数据属性,使用其
的toJSON()方法。此方法返回的属性作为副本
对象(不是一个JSON字符串尽管它的名字)。 (当JSON.stringify()是
传递的对象用的toJSON()方法,它stringifies返回
的toJSON的值()的,而不是原来的对象。在这些例子
previous节了这一功能时,他们被称为
JSON.stringify()登录模型实例)。
if you need to read or clone all of a model’s data attributes, use its toJSON() method. This method returns a copy of the attributes as an object (not a JSON string despite its name). (When JSON.stringify() is passed an object with a toJSON() method, it stringifies the return value of toJSON() instead of the original object. The examples in the previous section took advantage of this feature when they called JSON.stringify() to log model instances.)
<一个href=\"http://addyosmani.github.io/backbone-fundamentals/#backbone-basics\">http://addyosmani.github.io/backbone-fundamentals/#backbone-basics
谁能告诉我这两个psenting在 JSON
对象 办法再$ P $的区别符号。我只是困惑是否这些来达到同样的或有区别。
Can anyone tell me the difference between both these ways of representing an object
in JSON
notation. I am just confused whether these to achieve the same or there is a difference.
推荐答案
从<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON_behavior\">fine手动:
的toJSON行为
如果被字符串化一个对象有的toJSON
命名的属性,它的值是一个函数,那么的toJSON
法定制JSON字符串化的行为。而不是对象被序列化,被称为将被序列化时,的toJSON
方法的返回值
If an object being stringified has a property named toJSON
whose value is a function, then the toJSON
method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the toJSON
method when called will be serialized.
这就是为什么骨干使用 的toJSON
方法序列化和鉴于称为模型实例 M
,你可以说这样的话:
This is why Backbone uses the toJSON
method for serialization and given a model instance called m
, you can say things like:
var string = JSON.stringify(m);
和获取刚刚走出 M
的属性,而不是一堆噪音您的服务器不会在意。
and get just the attributes out of m
rather than a bunch of noise that your server won't care about.
这是说,主要的区别是,的toJSON
生成一个值(一个数字,布尔值,对象,...),获取转换成一个JSON字符串,而 JSON.stringify
总是产生一个字符串。
That said, the main difference is that toJSON
produces a value (a number, boolean, object, ...) that gets converted to a JSON string whereas JSON.stringify
always produces a string.
借助默认骨干的toJSON
只是这个(适用机型):
The default Backbone toJSON
is simply this (for models):
return _.clone(this.attributes);
所以 m.toJSON()
给你的浅的模型的属性的副本。如果有数组或对象的属性值,那么你将结束意想不到的参考共享。注意, Backbone.Model#克隆
还的从这个问题遭受
so m.toJSON()
gives you a shallow copy of the model's attributes. If there are arrays or objects as attribute values then you will end unexpected reference sharing. Note that Backbone.Model#clone
also suffers from this problem.
如果你想安全地克隆一个模型的数据,那么你可以通过 JSON.stringify
然后 JSON.parse $ C $发送c>来得到一个深度复制:
If you want to safely clone a model's data then you could send it through JSON.stringify
and then JSON.parse
to get a deep copy:
var data = JSON.parse(JSON.stringify(model_instance));
var cloned_model = new M(data);
其中, model_instance
是骨干模型实例 M
。
这篇关于的toJSON()和JSON.Stringify之间差()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!