描述

在面向对象编程中一个类定义了一个对象的特征. 类是定义 properties 和 一些方法, 是用来绘制具体对象实例的“蓝图”

ES6class 本质上就是一个语法糖,使我们编写构造函数更为方便

特性

  1. 在使用 function 写法时在 prototype 上定义方法是默认可枚举的,在使用 calss 写法时在 prototype 上定义方法是默认不可枚举的
  2. class 也存在暂时性死区的特性
  3. 取值函数和存值函数依然可以使用
  4. 默认使用严格模式
  5. 默认会有一个 constructor 不写不会报错
  6. new 方式来执行,不使用 new 执行则报错
  7. 内部使用 this 互相调用使用方法和属性

基本写法

  1. class Calculator { // 此时声明了一个构造函数 Calculator
  2. }

声明变量和方法

  1. class Calculator {
  2. // 声明了两个初始变量 num1 和 num2 初始值都为 undefin
  3. num1
  4. num2
  5. // 声明了一个初始变量 action 初始值为 'plus'
  6. action = 'plus'
  7. // 声明方法 plus
  8. plus() {
  9. return this.a + this.b
  10. }
  11. // 声明方法 sub
  12. sub() {
  13. return this.a - this.b
  14. }
  15. // 声明方法 mul
  16. mul() {
  17. return this.a * this.b
  18. }
  19. // 声明方法 div
  20. div() {
  21. return this.a / this.b
  22. }
  23. }

当然,也可以使用变量赋值来声明方法,箭头函数也是可以的

  1. class Foo {
  2. fn = function () { }
  3. arrowFn = () => { }
  4. }

类的初始化 constructor

  1. class Calculator {
  2. num1
  3. num2
  4. // 使用 constructor 来使得 class 被 new 时执行的程序
  5. constructor(num1, num2) {
  6. this.num1 = num1
  7. this.num2 = num2
  8. }
  9. }

静态方法和静态属性

使用 static 关键字来声明 内部的静态方法和属性

静态方法和静态属性无法被 实例化对象 调用

  1. class Calculator {
  2. // 声明了一个静态属性 actions 初始值为 ['plus', 'sub', 'mul', 'div']
  3. static actions = ['plus', 'sub', 'mul', 'div']
  4. // 声明了一个静态方法 round
  5. static round(value) {
  6. const type = typeof value
  7. switch (type) {
  8. case 'string':
  9. return Number(value).toFixed(0)
  10. case 'number':
  11. return value.toFixed(0)
  12. default:
  13. return Number(value).toFixed(0)
  14. }
  15. }
  16. }

使用 class

  1. class Calculator {
  2. // 声明了两个初始变量 num1 和 num2 初始值都为 undefin
  3. num1
  4. num2
  5. // 声明了一个初始变量 action 初始值为 'plus'
  6. action = 'plus'
  7. // 声明了一个静态属性 actions 初始值为 ['plus', 'sub', 'mul', 'div']
  8. static actions = ['plus', 'sub', 'mul', 'div']
  9. // 声明了一个静态方法 round
  10. static round(value) {
  11. const type = typeof value
  12. switch (type) {
  13. case 'string':
  14. return Number(value).toFixed(0)
  15. case 'number':
  16. return value.toFixed(0)
  17. default:
  18. return Number(value).toFixed(0)
  19. }
  20. }
  21. // 使用 constructor 来使得 class 被 new 时执行的程序
  22. constructor(num1, num2) {
  23. this.num1 = num1
  24. this.num2 = num2
  25. }
  26. // 声明方法 plus
  27. plus() { return this.num1 + this.num2 }
  28. // 声明方法 sub
  29. sub() { return this.num1 - this.num2 }
  30. // 声明方法 mul
  31. mul() { return this.num1 * this.num2 }
  32. // 声明方法 div
  33. div() { return this.num1 / this.num2 }
  34. run() {
  35. switch (this.action) {
  36. case 'plus':
  37. return this.plus()
  38. case 'sub':
  39. return this.sub();
  40. case 'mul':
  41. return this.mul()
  42. case 'div':
  43. return this.div()
  44. default:
  45. return this.plus()
  46. }
  47. }
  48. }
  49. // 使用 new 关键字来实例化 类(构造函数)
  50. const calculator = new Calculator(5, 10,)
  51. // 调用实例化对象的 run 方法
  52. console.log(calculator.run()) // 15
  53. // 更改实例化对象的 action 属性
  54. calculator.action = 'mul'
  55. // 调用实例化对象的 run 方法
  56. console.log(calculator.run()) // 50
  57. // 更改实例化对象的 action 属性
  58. calculator.action = 'div'
  59. // 调用实例化对象的 div 方法
  60. console.log(calculator.div()) // 0.5
  61. // 调用 类(构造函数) 上的 静态方法 round
  62. console.log(Calculator.round('10.99')) // 11
  63. // 访问 类(构造函数) 上的 静态属性 actions
  64. console.log(Calculator.actions) // ['plus', 'sub', 'mul', 'div']

属性的私有化

用在一些属性成员不想被外部访问的情况

  1. const drink = Symbol()
  2. function study() {
  3. console.log('I am studing');
  4. }
  5. class Person {
  6. // 相对私有化
  7. [drinkAction]() {
  8. console.log('I can drink');
  9. }
  10. // 绝对私有化
  11. study() {
  12. study.call(this)
  13. }
  14. }

访问和赋值的 getset

中依然可以使用 getset

  1. class Person {
  2. _age = 0
  3. get age() {
  4. return this._age++
  5. }
  6. set age(value) {
  7. if (value > 100) {
  8. console.log("年龄最大不得大于100岁")
  9. return false
  10. }
  11. if (value < 0) {
  12. console.log("年龄最小不得小于0岁")
  13. return
  14. }
  15. this._age = value
  16. }
  17. }
  18. var person = new Person()
  19. console.log(person.age) // 0
  20. console.log(person.age) // 1
  21. person.age = 101 // 年龄最大不得大于100岁
  22. console.log(person.age) // 2
  23. person.age = -1 // 年龄最小不得小于0岁
  24. console.log(person.age) // 3
  25. person.age = 55
  26. console.log(person.age) // 55

extends

extendsclass 继承的关键字

继承方式相对 function 来讲则要简单的许多,只需要使用 extends 关键即可

  1. class Father {
  2. work() {
  3. console.log('I am working')
  4. }
  5. }
  6. class Son extends Father {
  7. study() {
  8. console.log('I am studing')
  9. }
  10. }
  11. var son = new Son()
  12. son.study() // I am working
  13. son.work() // I am studing

关键字 super

如果在 子类中 使用了 constructor,则必须之中 super 函数,否则会报错

  1. class Parent {
  2. constructor(name = "parent") {
  3. this.name = name
  4. }
  5. }
  6. class Child extends Parent {
  7. // 如果写了 constructor 一定要写 super()
  8. constructor() {
  9. super() // super 不写则报错u
  10. }

new

在使用 class 时,必须通过 new 实例的方式去执行构造函数 否则会报错

  1. const obj = {
  2. }
  3. class Foo {
  4. constructor(name) {
  5. this.name = name
  6. }
  7. }
  8. // 以下几个执行均报错
  9. // Class constructor Foo cannot be invoked without 'new'
  10. Foo('nothing')
  11. Foo.call(obj)
  12. Foo.apply(obj)

function vs Class

声明

1.jpeg

属性的定义

2.jpeg

静态属性的定义

3.jpeg

方法的定义

4.jpeg

静态方法的定义

5.jpeg