Skip to content

Commit bc72b15

Browse files
Docs clarification and explanation JSON.stringify (#294)
1 parent 2de43f7 commit bc72b15

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

docs/JavaScript/JSON/stringify.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ A JSON string representing the given value, or undefined.
1616

1717
#### Stringify native objects
1818

19-
Minecraft has modified `JSON.stringify` function so that when stringify a native object, it always return `{}`.
19+
As you may have already noticed, when you `JSON.stringify()` native objects, the properties that this native object has are not displayed, this has the consequence that the `JSON.stringify()` function only returns an empty object `{}`.
20+
Why is it like this?
21+
The answer is quite obvious, native class instances have no properties, therefore they cannot be displayed, native classes have getters that are always called from the prototype of that instance, and J`JSON.stringify()` does not include prototypes, that's all, but this does not apply to interfaces, they do not have native prototypes therefore it is possible to display them as, for example, a `Vector3` interface.
2022

21-
A workaround is to deep copy the JSON object. Highly recommend using this package to display native objects in string.
23+
A workaround is to deep copy the JSON object. Highly recommend using this package to display class insatnce properties.
2224
- https://github.com/zxdong262/deep-copy
2325

2426
## Learn more

docs/JavaScript/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ These features exist in vanilla (most engine / frameworks) JavaScript global sco
3636
- [Infinity](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity)
3737
- [JSON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON)
3838
- [JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)
39-
- [JSON.stringify](./JSON/stringify.md) (Modified in Minecraft, click for more infomation.)
39+
- [JSON.stringify](./JSON/stringify.md) (Why can't stringify native objects? `{}`, click for more infomation.)
4040
- [undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)
4141
- [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)
4242
- [Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)

0 commit comments

Comments
 (0)