首页 > Web开发 > 详细

JS高级程序设计3

时间:2017-08-17 18:14:35      阅读:240      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/gyz418/p/7383213.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!