分析:
上代码:
function stringify(obj, fnOrArr) {
if (typeof obj === 'function') {
return undefined;
}
if (typeof obj === 'symbol') {
return undefined;
}
if (obj === undefined) {
return undefined;
}
if (typeof obj === 'number' && isNaN(obj)) {
return 'null';
}
if (obj === Infinity) {
return 'null';
}
if (obj === null) {
return 'null';
}
if (Object.prototype.toString.call(obj) === '[object Date]') {
return `'${ obj.toJSON() }'`;
}
if (Object.prototype.toString.call(obj) === '[object RegExp]') {
return '{}';
}
if (typeof obj !== 'object') {
return typeof obj === 'string' ? `'${ obj }'` : obj;
}
if (Array.isArray(obj)) {
const arr = obj.map((list) => {
if (list === undefined || typeof list === 'symbol' || typeof list === 'function') {
return 'null';
} else {
return `${ stringify(list) }`
}
});
return `'[${ arr.join(',')}]'`;
}
const keys = Object.keys(obj);
const objArr = keys.map((list) => {
// 数组内的 undefined、Symbol、Function 转为null
if (stringify(obj[list]) !== undefined && obj[list] !== undefined) {
return `"${ list }": ${ stringify(obj[list]) }`;
}
})
// 过滤undefined
return `{ ${ objArr.filter(list => list !== undefined).join(',') }}`;
}
示例:
const map = new Map();
map.set('name', 'Jerry');
const set = new Set();
set.add('age', 32);
const weakMap = new WeakMap();
const a = {};
weakMap.set(a, 'Female');
const weakSet = new WeakSet();
const b = {};
weakSet.add(b, 'Beijing');
const obj = {
regexp: new RegExp(),
today: new Date(),
fn: function (name) {
console.log('name:', name);
},
name: null,
age: undefined,
infinity: Infinity,
nan: NaN,
sex: Symbol('Male'),
staff: [ "Jerry",123, () => {}, Symbol("Sina"), undefined, null, NaN, ],
map: map,
set: map,
weakMap: weakMap,
weakSet: weakSet,
}
使用JSON.stringify得到的是:
'{"regexp":{},"today":"2023-06-27T02:51:41.607Z","name":null,"infinity":null,"nan":null,"staff":["Jerry",123,null,null,null,null,null],"map":{},"set":{},"weakMap":{},"weakSet":{}}'
使用stringify得到的是:
'{ "regexp": {},"today": '2023-06-27T02:51:41.607Z',"name": null,"infinity": null,"nan": null,"staff": '['Jerry',123,null,null,null,null,null]',"map": { },"set": { },"weakMap": { },"weakSet": { }}'
这个stringify不是很完善的一个方法,和原生JSON.stringify还是有一定的差距,JSON.stringify是可以接受三个参数的,我这边暂不支持,后续再加吧。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。