class 是 ES6 的新特性,可以用来定义一个类,实际上,class 只是一种语法糖,它是构造函数的另一种写法。class Person {}typeof Person // “function"Person.prototype.constructor === Person // true???? 使用用法和使用构造函数一样,通过 new 来生成对象实例class Person {}let jon = new Person()???? constructor每个类都必须要有一个 constructor,如果没有显示声明,js 引擎会自动给它添加一个空的构造函数:class Person {}// 等同于class Person { constructor () { }}???? 实例属性和方法,原型属性和方法定义于 constructor 内的属性和方法,即定义在 this 上,属于实例属性和方法,否则属于原型属性和方法。class Person { constructor (name) { this.name = name } say () { console.log(‘hello’) }}let jon = new Person()jon.hasOwnPrototype(’name’) // truejon.hasOwnPrototype(‘say’) // false???? 属性表达式let methodName = ‘say’class Person { constructor (name) { this.name = name } [methodName] () { console.log(‘hello’) }}???? 静态方法不需要通过实例对象,可以直接通过类来调用的方法,其中的 this 指向类本身class Person { static doSay () { this.say() } static say () { console.log(‘hello’) }}Person.doSay() // hello静态方法可以被子类继承// …class Sub extends Person {}Sub.doSay() // hello可以通过 super 对象访问// …class Sub extends Person { static nice () { return super.doSay() }}Sub.nice() // hello???? 严格模式不需要使用 use strict,因为只要代码写在类和模块内,就只能使用严格模式。???? 提升class 不存在变量提升。new Person() // Uncaught ReferenceError: Person is not definedclass Person {}???? name 属性name 属性返回了类的名字,即紧跟在 class 后面的名字。class Person {}Person.name // Person???? this默认指向类的实例。???? 取值函数(getter)和存值函数(setter)class Person { get name () { return ‘getter’ } set name(val) { console.log(‘setter’ + val) }}let jon = new Person()jon.name = ‘jon’ // setter jonjon.name // getter???? class 表达式如果需要,可为类定义一个类内部名字,如果不需要,可以省略:// 需要在类内部使用类名const Person = class Obj { getClassName () { return Obj.name }}// 不需要const Person = class {}立即执行的 Class:let jon = new class { constructor(name) { this.name = name } sayName() { console.log(this.name) }}(‘jon’)jon.sayName() //jon???? 参考《ECMAScript 6 入门》Class 的基本语法 by 阮一峰