ECMA Script 6_ 类 class

类 class编程

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

经过 class 关键字,能够定义类 classspa

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

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

  • 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对象

5blog

5继承

5原型

5io

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

相关文章
相关标签/搜索