关于 JSON,以下代码输出什么
更多描述
const obj = {
a: 3,
b: 4,
c: null,
d: undefined,
get e() {},
};
console.log(JSON.stringify(obj));Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 149
Author 回答者: shfshanyue
const obj = { a: 3, b: 4, c: null, d: undefined, get e() {}, };console.log(JSON.stringify(obj))
输出什么?
{"a":3,"b":4,"c":null}对其中的 undefined,function 将在 JSON.stringify 时会忽略掉
Author 回答者: qiushangzhe
666
const obj = { a: 3, b: 4, c: null, d: undefined, get e() {}, };console.log(JSON.stringify(obj)) 输出什么?
{"a":3,"b":4,"c":null}对其中的
undefined,function将在JSON.stringify时会忽略掉
const obj 中的 get e () {} 并不是函数,此处应该是重写了 obj.e 的 get 方法,因为 get 方法未定义返回值,因此在执行 JSON.stringify 时,执行 obj.e 的 get 方法,返回 undefined,因此被忽略
让我们更改 get 方法的返回值

看到 666 了吧?
Author 回答者: AlanReumy
const obj = { a: 3, b: 4, c: null, d: undefined, get e() {}, };console.log(JSON.stringify(obj)) 输出什么?
{"a":3,"b":4,"c":null}对其中的
undefined,function将在JSON.stringify时会忽略掉
const obj中的get e () {}并不是函数,此处应该是重写了obj.e的get方法,因为get方法未定义返回值,因此在执行JSON.stringify时,执行obj.e的get方法,返回undefined,因此被忽略让我们更改
get方法的返回值看到 666 了吧?
原来是这样,又学到了