ES6 语法笔记

    //如果使用let,声明的变量仅在块级作用域内有效
        {
            var a = 1;
            let b = 2;
        }

        console.log(a);  // 1
        console.log(b);  // Uncaught ReferenceError: b is not defined

        var arr = [1,2,3]

        for (let i = 0, l = arr.length; i < l; ++i) {}

        console.log(i);

    //上面代码中,变量i是let声明的,当前的i只在本轮循环有效
        
        var arr = [];
        for (let i = 0, l = 10; i < l; ++i) {
            arr[i] = function() {
                console.log(i);
            }    
        }

        arr[6]();  //?
    
    //不存在变量提升
        
        console.log(foo);
        console.log(bar);  //报错

        var foo = 2;
        let bar = 2;

    //块级作用域和 函数声明
    function f() { console.log('I am outside!'); }
    (function () {
      f();
    }());


    function f() { console.log('I am outside!'); }
    (function () {
      
      if (false) {
        function f() { console.log('I am inside!'); }
      }

      f();
    }());  

    //函数参数的解构赋值
    function add([x, y]){
      return x + y;
    }

    var result = add([1, 2]); // 3

    console.log(result);

        // es6 的 类的概念
        class Point {
          constructor(x, y) {
            this.x = x;
            this.y = y;
          }

          toString() {
            return '(' + this.x + ', ' + this.y + ')';
          }
        }        

        var point = new Point(1,2);
        console.log(point.toString());

        //可以写出 立即执行的 Class 类
        
        let person = new class {
          constructor(name) {
            this.name = name;
          }

          sayName() {
            return this.name;
          }
        }('张三');

        const result = person.sayName(); // "张三"

        console.log(result);    
posted @ 2016-06-29 17:53  SmallW  阅读(254)  评论(0编辑  收藏  举报