码迷,mamicode.com
首页 > 其他好文 > 详细

ECMAScript 5

时间:2016-11-09 19:19:20      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:处理   console   return   one   his   lib   har   元素   sof   

ECMAScript 5  Object Array扩展

技术分享
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>ECMAScript5</title>
    <style>
        p{  line-height: 40px;border-bottom: 1px solid green;}
    </style>
</head>
<body>
     <p>1 .ES5 strict模式,变量必须有 var</p>
<script>
    "use strict";
     //testvar = 4;
</script>
<p>2. JSON.parse 字符串转为json</p>
     <script>
         "use strict";
         var str={"name":"hello"};
         var res=JSON.parse(str);
         console.log("res.name: "+res.name);
     </script>
<p>2.2 JSON.parse reviver</p>
     <script>
         "use strict";
         var str2={"name":"hello","age":"12"};
         var res2=JSON.parse(str2,function(key,value){
             if(typeof value == string){
                 return parseInt(value);
             }else{
                 return value;
             }
         });
         console.log("res2.age: " +res2.age+  ,type: +typeof(res2.age));
     </script>
<p>3. JSON.stringfy() 过滤掉 undefined 的值 ,并格式化JSON 缩进</p>
     <script>
         "use strict";
          var nums = {
             "first": 7,
             "second": 14,
             "third": 13
         }
         var luckyNums = JSON.stringify(nums, function(key, value) {
             if (value == 13) {
                 return undefined;   // 如果 undefined 则直接被JSON忽略。
             } else {
                 return value;
             }
         }, 2);  // 数字2是格式化,缩进2个空格,最大缩进10个空格。
         console.log("luckyNums: "+luckyNums);
     </script>
<p>4.1 Object.defineProperty 对象属性 ,定义 cat 对象的 name 属性 默认值为 miao 且不可以修改</p>
     <script>
         "use strict";    // 去掉 strict 不报错。 但可以显示错误
         var cat={};
         Object.defineProperty(cat,"name",{
             value:miao,
             writable:false
         })
         //cat.name=‘hehe‘;   // use strict 浏览器会报错
         console.log("cat.name: "+cat.name);
     </script>
<p>4.2 Object.create()</p>
     <script>
         "use strict";
         function Poker(style, title, value) {
             this.Style = style;
             this.Title = title;
             this.Value = value;
         }
         var pokerA = Object.create(new Poker("club", "A", 14));
         console.log(Poker.constructor); //function Function() { [native code] }
         console.log("pokerA.Value: "+pokerA.Value);
     </script>
<p>4.3 Object 总结:对对象的属性有了更多控制,比如是否允许被修改,删除等</p>
     <a href="https://msdn.microsoft.com/zh-cn/library/dn656907.aspx">Object细节</a>
<p>5.1 Array 数组 isArray indexOf</p>
     <script>
         "use strict";
         console.log("Array.isArray(): "+Array.isArray("xxx"));
         console.log("Array.isArray(): "+Array.isArray([a,b]));
         console.log("Array.indexOf(): "+["Hello", "javaScript", "ECMAScript", "HTML5"].indexOf("javaScript"));//1
         console.log("Array.lastIndexOf(): "+["a","Hello", "javaScript"].lastIndexOf("javaScript"));//1
     </script>
<p>5.2 array.forEach 处理数组每一个元素</p>
     <script>
         "use strict";
         var arr1 = "Charles,Mark,Bill,Vincent,William,Joseph".split(",");
         arr1.forEach(
                 function(item, index) {
                     if (index % 2 == 0) {
                         arr1[index] = "name:" + item;
                     }
                 }
         );
         arr1.forEach(
                 function(item, index) {
                     console.log("array.forEach(): "+item);
                 }
         );
         </script>
<p>5.3 array.some()  一些   array.every()</p>
     <script>
         var arr11 = "Charles,Markee,Bile,Vincent,William,Joseph".split(",");
         console.log("array.some(): "+arr11.some(
                         function(item, index) {
                             return item.length < 5;
                         }   //数组是否有一些元素的长度小于5  true
                 )
         ); //true
         console.log("array.every(): "+arr11.every(
                         function(item, index) {
                             return item.length < 7;
                         }   // 所有数组元素长度都小于7 ?   false
                 )
         )//false
     </script>
<p>5.4 array.map()</p>
     <script>
         var arr1 = "Charles,Mark,Bill,Vincent,William,Joseph".split(",");  // split() 字符串转成数组
         var arr2 = arr1.map(
                 function(item, index) {
                     if (item.indexOf("ll") > -1) {
                         return item;
                     }
                 }
         );
         arr2.forEach(
                 function(item, index) {
                     console.log("item: "+item);
                 }
         );
     </script>
</body>
</html>
View Code

 

ECMAScript 5

标签:处理   console   return   one   his   lib   har   元素   sof   

原文地址:http://www.cnblogs.com/gyz418/p/6047866.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!