class

2017-11-13  本文已影响0人  秦小婕

1.语法

  //定义类
     class Point {
        constructor(x, y) {
          this.x = x;
          this.y = y;
        }

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

用** Object.getPrototypeOf()** 方法来获取实例对象的原型.

2.不存在变量提升

  函数调用要在定义的后面

3.私有方法

4.私有属性#X

5.this的指向

对象方法中的this指向引用方法的对象。如果对象中的方法被单独拎出来调用,则this指向执行环境。为了解决这个问题,要将方法中的this进行绑定

6.class中的generator函数

    class Foo {
      constructor(...args) {
      this.args = args;
    }
   * [Symbol.iterator]() {
      for (let arg of this.args) {
         yield arg;
      }
   }
 }

 for (let x of new Foo('hello', 'world')) {
    console.log(x);
 }
// hello
// world

Symbol.iterator方法返回一个Foo类的默认遍历器,for...of循环会自动调用这个遍历器。

7.class中的静态方法

方法前加关键字static。
类的静态方法不会被实例所继承。
父类的静态方法可以被子类所继承

8.new.target

9.

上一篇下一篇

猜你喜欢

热点阅读