front-end-interview-questions
front-end-interview-questions copied to clipboard
【手写实现题目】JSON.stringify
let student = {
name: 'John',
age: 30,
isAdmin: false,
courses: ['html', 'css', 'js'],
wife: null,
sayHi() {
// 被忽略
alert('Hello');
},
[Symbol('id')]: 123, // 被忽略
something: undefined, // 被忽略
};
let str = JSON.stringify(student);
console.log(str);
完整的语法 let json = JSON.stringify(value[, replacer, space])
let user = {
name: "John",
age: 25,
roles: {
isAdmin: false,
isEditor: true
}
};
alert(JSON.stringify(user, null, 2));
/* 两个空格的缩进:
{
"name": "John",
"age": 25,
"roles": {
"isAdmin": false,
"isEditor": true
}
}
*/
/* 对于 JSON.stringify(user, null, 4) 的结果会有更多缩进:
{
"name": "John",
"age": 25,
"roles": {
"isAdmin": false,
"isEditor": true
}
}
*/