// JSON.stringfiy模拟
// 考虑类型,boolean,number,string,function 与 array,object 区分
// 忽略类型undefined,null,symbal
// 需要判断前后加", undefined,string,function
function jsonStringfiy (value){
let val = typeof value
// console.log(val)
if(val !== 'object'){
if(/undefined|string|function/.test(val)){
value = '"' + value + '"'
}
return String(value)
}else{
let json = []
let isA = Array.isArray(value)
for(let k in value){
let v = value[k]
if(/undefined|string|function/.test(typeof v)){
v = '"' + v + '"'
}else if (typeof v === 'object'){
v = jsonStringfiy(v)
}
// console.log(v)
json.push((isA ? "" : '"' + k + '":')+ String(v))
}
return (isA?'[':'{') + String(json) + (isA?']':'}')
}
}
let a = [1,2,3,4,[5,6,7]]
let b = {a:1,b:[1,'9',3],c:false}
let c = 9
console.log(jsonStringfiy(a))
console.log(jsonStringfiy(b))
console.log(jsonStringfiy(c))
console