ecma5标准

ecma5 标准 第五版
         ecma6 严格模式 
   function max(){
            m1 = 10;//没有var,变量被当作全局变量来使用,
        }
        function max(){
            "use strict"
            m1 = 10;//严格模式下,没有var,则报错,严格模式只在当前作用域下起作用,不要轻易在全局模式下使用严格模式
        }
         indexOf();数组, indexOf(元素,开始的下标);
         forEach();遍历数组  arr.forEach(function(当前遍历到的元素,当前遍历的下标,当前数组){});
    var arr = [10,20,30];
       arr.forEach(function(item,index,arrey){
           alert(item + "," + index + "," + arrey);});//返回10,0,10,20,30,依次循环3次
           arr.forEach(alert);//返回三次10,20,30
         map(); arr.map(function(当前遍历到的元素,当前遍历的下标,当前数组),遍历》操作》返回 
    arr.map(function(item,index,arrey){              //遍历
      return item + 2;});//操作返回
         reduce();归并,  arr.reduce(function(pre,next,index,arrey);  pre上一次遍历元素返回的值, next当前遍历的元素,,如果 reduce传入了参数,则 pre变成参数, next成为第一个元素
  arr.reduce(function(){
    alert(pre + "," + next);
    return pre + next;
    });//第一次返回10,20,第二次返回30,30
         filter();过滤 arr.filter(function(当前遍历到的元素,当前遍历的下标,当前数组),遍历》操作》返回
    var arr2 = arr.filter(function(item,index,arrey){              //遍历
       return item > 10;});//操作大于10的元素返回
         some();操作返回 truefalse
      arr.some(function(item,index,arrey){             // 遍历
      return item == 10;});//操作相等10的元素返回true ,false,匹配成功,则不进行后面的判断    
         every();some类似,但是每一项都要符合才返回 true
         Math对象;用于执行数学任务,常用, Math.PI 3.14159
             round();四舍五入     Math.round(3.4); 返回3
             random();
             max();返回较大值
             min();返回较小值
             abs();返回这个数的绝对值,  Math.abs(-10); 返回10
             ceil();向上取整,  Math.ceil(3.1); 返回4
             floor();向下取整
             pow();几次方,  pow(x,y); x的y次方
             sqrt();开平方,  Math.sqrt(4); 返回2
             勾股定理:
             Math.sin();Math.cos();Math.tan();正弦/余弦/正切,参数都是弧度, Math.PI = 180弧度1弧度 = Math.PI / 180
                  alert(Math.sin(30 * Math.PI / 180)); 30度的sin值,应该是0.5,计算是0.4999,这是计算机本身的bug,没办法解决,银行的计数是分,没有小数点。
             对象:是一种类型,复合数据类型/引用类型,方法名称是地址,通常也称为类, ECMAscript虽然是面向对象的语言,但是却不具备传统语言所支持的类的数据结构
                对象存储的数据,称属性,对象存储的函数,称方法
         var person = new object();//创建,也可以省略new,
         var person = {};//和上面相同
         person.name = "dsf";//添加属性
         person["name"] = "fd";//和上面相同
         person.showName = function(){alert(person.name);}//添加方法
         delete person.name;//delete关键字
             日期对象
    var d =new Date();//返回一个当前时间,
       var d = new Date("2015/5/22");//可以传入参数,"2011-2-3",2011,03,03,14,23  传入毫秒数,从1970 unix诞生日开始算起,1000,
             日期的几个方法
        d.toDateString();
     d.toLocalDateString();
                获取一周的某一天,某月都是从0开始的下标,星期天代表0,  d.getDay(); 星期天返回0
                 Date.parse("2015-2-3"); 日期格式化,返回值,1970以来的毫秒数,
                 d.getTime()/d.setTime(),获取设置自1970年以来的毫秒数,
        function showDate(n){
             var d = new Date();//创建一个对象
             var da =d.getDate(); //获取当前日期
             d.setDate(da + n);//加上一个参数日期
              return d;//返回日期
         }
         alert(showDate(28));    
                 setInterval(函数,毫秒数);定时器功能,每隔设置毫秒数启动一次函数,  setInterval(show,1000); 如果用按钮来控制定时器,多次点击按钮将会重复启动定时器,造成返回值id变化
                 clearInterval(定时器ID);var id = 定时器获取返回值
                 innerHtml;标签间的所有内容,
     var oDiv = document.getElmentById("div1");
     oDiv.innerHtml = "<h3>我是替换文本</h3>";
        秒表的实现
  html body{margin: 0}
        #div1{height: 350px;width: 200px;background-color: #999;margin:100px auto;text-align: center}
        #count{height: 200px;width: 100%;background-color: #888;line-height: 200px;font-size: 40px;}
        #div1 input{display: block;margin: 5px auto;width: 80px;height: 40px;}
        span{background-color: #777;width: 40px;height: 20px;}
    <div id="div1">
        <div id="count">
            <span id="h"></span>
            <span id="m"></span>
            <span id="s"></span>
        </div>
        <input id="start" type="button" value="开始">
        <input id="pause" type="button" value="暂停">
        <input id="stop" type="button" value="停止">
    </div>
//封装函数,启用$符号 
      function $(id){
            return document.getElementById(id);
        }
//封装函数,让秒数在小于10的时候,十位数显示0.
        function sz(cou){
            if(cou < 10){
                return "0" + cou; 
            }
            return cou;
        }
        window.onload = function(){
            //开始计数
            var count = 0;//设定一个数字,让数字自我增加
            var timer = null;//设置一个调用函数的id,可以清除id,而不重复调用函数
            $("s").innerHTML = "00";//让标签显示秒
            $("m").innerHTML = "00";//让标签显示分
            $("h").innerHTML = "00";//让标签显示小时
            $("start").onclick = function(){//点击按钮的时候开始
                timer = setInterval(function(){//计时器设定调用函数为1秒
                    count++;
                    $("s").innerHTML = sz(count % 60);
                    $("m").innerHTML = sz(parseInt(count / 60) % 60);
                    $("h").innerHTML = sz(parseInt(count / 3600) % 24);
                },1000);
            }
            //暂停
            $("pause").onclick = function(){
                clearInterval(timer);
            }
            //停止,数据清理全部归0
            $("stop").onclick = function(){
                clearInterval(timer);//清除调用函数,
                count = 0;
                $("s").innerHTML = "00";
                $("m").innerHTML = "00";
                $("h").innerHTML = "00";
            }
        }

猜你喜欢

转载自www.cnblogs.com/solaris-wwf/p/11622249.html