码迷,mamicode.com
首页 > 编程语言 > 详细

03JavaScript程序设计修炼之道 2019-06-09_fe-js-053Math对象_ Date对象、es6对象新变化

时间:2019-07-07 17:36:44      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:flow   family   end   api   mil   lib   func   margin   hid   

36Date.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // Date 日期时间
        var d = new Date();
        //console.log(d.toString());
        //console.dir(d);
        console.log(d.getFullYear());//
        console.log(d.getMonth()); // 0-11 0表示1月份
        console.log(d.getDate()); //
        console.log(d.getDay()); // 星期 0-6  0周日
        console.log(d.getHours(),d.getMinutes(),d.getSeconds());
       // 2019年06月11日 20:23:41 星期二
       //console.log(d.toLocaleString());
       function dateToString(d) {
           var year = d.getFullYear();
           var month = toTwo(d.getMonth() + 1);
           var day = toTwo(d.getDate());
           var h = toTwo(d.getHours());
           var m = toTwo(d.getMinutes());
           var s = toTwo(d.getSeconds());
           var arr = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
           var weekday = arr[d.getDay()];
           var str = `${year}年${month}月${day}日 ${h}:${m}:${s} ${weekday}`;
           return str;
       }

       function toTwo(v) {
            return v<10?‘0‘+v:v;
       }

       console.log(dateToString(d));
    </script>
</body>
</html>

37Date-2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        var str  = "2000/12/12"; // 
        //var d = new Date();// 系统时间
        /* 字符串转成日期格式
        var d1 = new Date(str);
        console.dir(d1);

        var num = Date.parse(str); // 时间戳
        var d2 = new Date(num);
        console.log(d2.toLocaleString());

        function stringToDate(str) {
            return new Date(str);
        }
        */


        // 10天后的日期
        //d.setDate(d.getDate()+10);
        //console.log(d);
        
        /*
        console.log(d.getTime());
        d = new Date(d.getTime()+10*24*3600*1000);
        console.log(d);
        */
        var d = new Date();
        var d2 = new Date("2019/10/1");
        function diff(start,end) {
            return (end.getTime() - start.getTime())/1000
        }
        // 6 11 
        var days = parseInt(diff(d,d2)/24/3600);
        console.log(days);
    </script>
</body>
</html>

38object-es6.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // 1 对象的解构赋值
        var obj = {name: "along", age: 32};
        //var  {name,age}   = obj;
        // 旧名字(右边对象的属性名):新名字
        var {name2:userName,age:userAge}   = obj;
        console.log(userName);

        function ajax({method="get",data={}}) {
            console.log(method,data);
        }
        
        /* 以前做法
        function ajax(options) {
            var method = options.method || ‘get‘;
            var data = options.data || {};
        }
        */

        ajax({
            data: {}
        });

        function fn(a=10,b=20) {

        }

        // 2 对象属性或方法简写
        var name = "along";
        var age = 32;
        var teacher = {
            name: name, // name: "along"
            age: age,
            teach: function() {
                console.log("hard working");
            }
        };

        //简写
        var teacher2 = {
            name, // name: "along"
            age,
            teach(){
                console.log(arguments);
                console.log("hard working");
            }
        };
        console.log(teacher2.name,teacher2.age);
        teacher2.teach();

        // 3 新增api
        //Object.is()
        console.log( NaN === NaN); // false
        console.log(+0 === -0); // true
        
        // [NaN,19,NaN]
        console.log(Object.is(NaN,NaN)); // true
        console.log(Object.is(+0,-0)); // false

        // Object.assign()
        var obj1 = {x:10, y:20};
        var obj2 = {z:30};
        Object.assign(obj2,obj1);
        console.log(obj2);
        console.log(obj1);
        
        // 克隆对象 浅层复制
        function clone(obj) {
            return Object.assign({},obj);
        }
        var obj3 = {a:10,b:{num:100}};
        var obj4 = clone(obj3);
        obj4.b.num = 200;
        console.log(obj3.b.num);

        var obj5 = {};
        for(var key in obj3) {
            obj5[key] = obj3[key];
        };
        console.log(obj5);

        // Object.keys() Object.values()
        var object = {a:1,b:2,c:3};
        console.log(Object.keys(object)); //["a", "b", "c"]
        console.log(Object.values(object));

        // for of
        for(var v of Object.keys([1,2,3])) {
            console.log(v);
        }

        // 原型 super 第三季   
    </script>
</body>
</html>

 

 

 

 

 

03JavaScript程序设计修炼之道 2019-06-09_fe-js-053Math对象_ Date对象、es6对象新变化

标签:flow   family   end   api   mil   lib   func   margin   hid   

原文地址:https://www.cnblogs.com/HiJackykun/p/11146723.html

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