標籤:play none round 定義資料 author logs es6 class key
PS:有一小部分寫在了 JS 2017了
JSON
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title></head><body>IE8支援 JSON.stringify()<script> var book = { "title":"js", "author":"kang", "edition":3 } // 1.過濾結果 var res =JSON.stringify(book,[‘title‘,‘author‘]) console.log(res); console.log(JSON.parse(res).title); // 2 自訂資料 var res2=JSON.stringify(book,function (key, val) { switch (key){ case "title": return val+ ‘ es6‘ case "author": return ‘jia‘ case "edition": return undefined default: return val } }) console.log(res2); // 3 格式化 var res3=JSON.stringify(book,[‘title‘],2) // 第3個值是格式化屬性,可以為數字,代碼縮排的空格數,如果是字串,則是用字串代替空格來縮排 // var res3=JSON.stringify(book,[‘title‘],‘---‘) console.log(res3); // 4 toJSON 不實用 var book2 = { "title":"es5 es6", "year":2017, toJSON:function () { return this.title } } console.log(book2.toJSON()); // es5 es6</script></body></html>
View Code
JS進階程式設計3