ECMA Script 6_ 类 class

类 class

ES6 提供了更接近传统语言的写法,引入了 Class(类)这个概念,作为对象的模板。

通过 class 关键字,可以定义类 class

新的 class 写法只是让对象原型的写法更加清晰、更像面向对象编程的语法而已

类的所有方法都定义在类的 prototype 属性上面,constructo() 也是类原型的 constructor()

  • class Point {
        constructor(x, y) {    /* 构造方法,实例  = new 类 */
            this.x = x;
            this.y = y;
        };
    
        toString() {
            return '(' + this.x + ', ' + this.y + ')';
        };
    };
    
    /**** 子类 继承 父类 ****/
    class myPoint extends Person {
        constructor(x, y, color){    /* 构造方法如果不写,系统默认也会创建,并在内调用 super() */
            super(x, y);    /* 调用父类的构造方法,继承父类的 属性 和 方法 */
            this.color = color;
        };
        
        sayIt(){    /* 不再需要 function 关键字 */
            return 'This is a '+color+'point('+x+', '+y+')';
        };
    };
  • 类的内部所有定义的方法,都是不可枚举的(non-enumerable)
  • 2

4

5

5

5

5

5

5

5

5

5

5

5

5

5

5

55

5

5

5

5

5

5

5

5

5

55

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

5

posted @ 2018-12-17 10:49  耶梦加德  阅读(205)  评论(0编辑  收藏  举报