jQuery.parseJSON的重点是什么(JSON.stringify(someData));

时间:2022-05-22 03:49:41

Came across this snippet

遇到了这个片段

var someDataJSON = jQuery.parseJSON(JSON.stringify(someData));

According to MDN the inner "method converts a JavaScript value to a JSON string"; then the outer method "Takes a well-formed JSON string and returns the resulting JavaScript value" per jQuery

根据MDN,内部“方法将JavaScript值转换为JSON字符串”;然后外部方法“获取格式正确的JSON字符串,并返回每个jQuery的结果JavaScript值”

If you start with a JS value & end up with a JS value, is this a pointless operation?

如果你从一个JS值开始并以JS值结束,这是一个毫无意义的操作吗?

2 个解决方案

#1


4  

Usually that is a trick used to get a by-value copy of an object in javascript. (Since all objects are passed by reference). You can find a more in-depth answer on how to accomplish this, if you're curious, in this * post

通常,这是用于在javascript中获取对象的按值副本的技巧。 (因为所有对象都通过引用传递)。如果您有兴趣,可以在此*帖子中找到有关如何完成此操作的更深入的答案

#2


2  

If someDataJSON is a flat JSON object, this way you get a copy of that object. Since, there is no direct method to copy a javascript object "by value" [and not "by reference"], this trick can be used.

如果someDataJSON是一个平面JSON对象,这样您就可以获得该对象的副本。因为,没有直接的方法来复制javascript对象“按值”[而不是“通过引用”],可以使用这个技巧。

var copyJSONObj = JSON.parse(JSON.stringify(JSONObj))

So, there is some point after all.

所以,毕竟有一点意义。

#1


4  

Usually that is a trick used to get a by-value copy of an object in javascript. (Since all objects are passed by reference). You can find a more in-depth answer on how to accomplish this, if you're curious, in this * post

通常,这是用于在javascript中获取对象的按值副本的技巧。 (因为所有对象都通过引用传递)。如果您有兴趣,可以在此*帖子中找到有关如何完成此操作的更深入的答案

#2


2  

If someDataJSON is a flat JSON object, this way you get a copy of that object. Since, there is no direct method to copy a javascript object "by value" [and not "by reference"], this trick can be used.

如果someDataJSON是一个平面JSON对象,这样您就可以获得该对象的副本。因为,没有直接的方法来复制javascript对象“按值”[而不是“通过引用”],可以使用这个技巧。

var copyJSONObj = JSON.parse(JSON.stringify(JSONObj))

So, there is some point after all.

所以,毕竟有一点意义。