CHANSHIYU
GITHUBZERO
  • README
  • 時雨
    • 2017
      • 01 网站动态标题的两种方式
      • 02 RN App 外部唤醒踩坑记
    • 2018
      • 01 不一样の烟火
      • 02 Python 之禅
      • 03 Python 文件操作
    • 2019
      • 01 Aurora 食用指南
      • 02 Godaddy 域名找回记事
      • 03 一个接口的诞生
      • 04 SpringMVC 前后端传参协调
      • 05 主题集成友链访问统计
      • 06 Github Style 博客主题
      • 07 字符编码の小常识
      • 08 WSL 安装 Docker 实录
      • 09 Eriri comic reader
      • 10 Aurora 2.0
      • 11 jsDelivr 全站托管
      • 12 两年工作台变迁史
      • 13 春物
      • 14 一种优雅の笔记方式
    • 2020
      • 01 Telegram 电报机器人
      • 02 她的眼里有星辰
      • 03 文心雕龙
      • 04 软萌木子の有趣笔谈
      • 05 Telegram RSS 订阅频道
      • 06 水月雨银色飞船
      • 07 五年前旧照
    • 2021
      • 01 春宵苦短 2020
      • 02 风花
    • 2022
      • 01 小城新貌
      • 02 原神满级纪念
    • 2023
      • 01 2022 逆旅
      • 02 半透明背景图实现
      • 03 新年攒台海景房
  • 前端
    • JavaScript
      • 01 JavaScript 秘密花园
      • 02 JavaScript 正则技巧
      • 03 从浏览器解析 JS 运行机制
      • 04 Canvas 基础用法
      • 05 Blob Url And Data Url
      • 06 函数节流与函数防抖
      • 07 排序算法初探
      • 08 洗牌算法实现数组乱序
      • 09 正则匹配 match 和 exec
      • 10 正则匹配汉字
      • 11 JSX.Element vs ReactElement
      • 12 可选链与空值合并
      • 13 TypeScript 编码规范
      • 14 Typescript 中 interface 和 type 区别
      • 15 TypeScript 高级类型
      • 16 TypeScript 关键字
      • 17 TypeScript 映射类型
    • CSS
      • 01 Flex 弹性布局
      • 02 Position 定位
      • 03 CSS 逻辑属性
    • Node
      • 01 Node Tips
      • 02 七天学会 NodeJS
    • Note
      • 01 Note
      • 02 Code
      • 03 Snippets
      • 04 Git
    • React
      • 01 React Props Children 传值
      • 02 Use a Render Prop!
      • 03 React Hook
      • 04 React Hook 定时器
      • 05 Fetch data with React Hooks
      • 06 React 和 Vue 中 key 的作用
      • 07 useCallback 的正确使用方式
      • 08 useLayoutEffect 和 useEffect 的区别
      • 09 forwardRef 逃生舱
      • 10 React 条件渲染
    • Vue
      • 01 Vue Tips
      • 02 Vue 构建项目写入配置文件
      • 03 Vue 项目引入 SVG 图标
      • 04 Vue 一键导出 PDF
      • 05 动态可响应对象
      • 06 Vue 引入 SCSS
      • 07 Vue 路由权限控制
    • 实战系列
      • 01 WebSocket 心跳重连机制
      • 02 图片加解密二三事
      • 03 优雅实现 BackTop
      • 04 动态加载 JS 文件
      • 05 常用 DOM 方法比较
      • 06 AbortController 中断 fetch
      • 07 计算字符所占字节数
      • 08 Axios 自定义返回值类型
  • 后端
    • Java
      • 01 面向对象基本特征与原则
      • 02 Java 数据类型
      • 03 Java String
      • 04 Java 只有值传递
      • 05 Java final 与 static
      • 06 Java Object 通用方法
      • 07 Java 继承
      • 08 Java 反射
      • 09 Java 异常
      • 10 Java 容器
      • 11 Java 虚拟机
      • 12 Java IO
      • 13 Java HashMap
      • 14 Java List
      • 15 Java Stream
      • 16 Java 枚举
      • 17 Java 日期与时间
      • 18 Java fail fast
      • 19 Java BiFunction 和 BinaryOperator
    • 并发编程
      • 01 Java 并发
      • 02 synchronized
      • 03 volatile
      • 04 ReentrantLock
      • 05 ReadWriteLock
      • 06 StampedLock
      • 07 CompletableFuture
      • 08 ForkJoin
      • 09 ThreadLocal
      • 10 CountDownLatch
      • 11 ThreadPoolExecutor
      • 12 ExecutorService
      • 13 Atom 原子类
      • 14 BlockingQueue
    • 高效编程
      • 01 30 seconds of java8
      • 02 函数式替代 for 循环
      • 03 Java 字符串拼接
      • 04 单例模式的几种实现
      • 05 HashMap 排序
    • 理论概念
      • 01 Java Servlet
      • 02 Java 服务端分层模型
      • 03 经典排序算法
      • 04 LRU 缓存淘汰算法
      • 05 BloomFilter 判断元素存在
      • 06 Java HashMap 面试大全
      • 07 HTTP 状态码详解
      • 08 Cookie 和 Session
      • 09 基于消息队列的分布式事务解决方案
      • 10 微服务之所见
    • 实战系列
      • 01 AES CBC 加解密
      • 02 Magic 魔数获取文件类型
      • 03 获取请求 IP 地址
      • 04 Kaptcha 与数学公式验证码
      • 05 Netty 获取客户端 IP.md
      • 06 高性能无锁队列 Disruptor.md
      • 07 前后端接入阿里云盾
    • Linux
      • 01 Linux 文件权限系统
      • 02 Linux 常用软件安装
      • 03 CentOS 防火墙
    • MySQL
      • 01 MySQL
      • 02 SQL 语句 where 1=1
      • 03 truncate 和 delete
      • 04 事务
      • 05 关系模型
      • 06 Mybatis
      • 07 MySQL 查看数据库表详情
    • Nginx
      • 01 Nginx 指北
      • 02 nginx gzip 压缩
    • Note
      • 01 Vagrant
      • 02 Docker
      • 03 Lombok
      • 04 Swagger
      • 05 Redis
    • Spring
      • 01 Spring Boot
      • 02 Spring Validation
      • 03 Spring Data
      • 04 Spring 容器
      • 05 Spring AOP
      • 06 Spring Transactional 注解
      • 07 Spring Cloud Netflix
      • 08 Spring Cloud Alibaba
      • 09 Spring Security oAuth2
      • 10 Spring Boot 跨域解决方式
      • 11 Spring Boot 请求拦截
      • 12 Spring Boot 异步编程
      • 13 Spring Boot 定时任务
      • 14 Spring Boot 管理 bean
      • 15 Mybatis 逆向代码生成
      • 16 JWT
      • 17 JPA
      • 18 Apache Shiro
      • 19 Spring 异步请求
  • 书斋
    • ES6 标准入门
      • 01 变量声明与解构赋值
      • 02 语法的扩展
      • 03 数据类型与数据结构
      • 04 Proxy 和 Reflect
      • 05 异步编程 Promise
      • 06 Iterator 和 for of 循环
      • 07 Generator 函数
      • 08 Async 函数
      • 09 Class 类
    • JavaScript 设计模式
      • 01 基础知识
      • 02 设计模式(上)
      • 03 设计模式(下)
      • 04 设计原则和编程技巧
  • 纸函
    • 01 Interview
    • 02 Ceph
    • 03 动态规划
    • 04 Document.designMode
    • 2023-01-10
  • 万藏
    • 文档
      • 01 Git 文档
      • 02 Linux 命令大全
      • 03 七天学会 NodeJS
      • 04 Algorithms
    • 工具
      • 01 Nginx Config
      • 02 ProcessOn
      • 03 Flat Icon
      • 04 Regexper
      • 05 TempMail
      • 06 Carbon
由 GitBook 提供支持
在本页
  • Class 的基本语法
  • 简介
  • constructor
  • Class 表达式
  • 不存在变量提升
  • 私有方法
  • this 指向
  • new.target
  • Class 的继承
  • 简介
  • Object.getPrototypeOf()
  • super 关键字
  • prototype 和 proto
  • Mixin 模式

这有帮助吗?

  1. 书斋
  2. ES6 标准入门

09 Class 类

直至 ES6,JavaScript 终于有了“类”的概念,它简化了之前直接操作原型的语法,也是我最喜欢的新特性之一,但此类非彼类,它不同于熟知的如 Java 中的类,它本质上只是一颗语法糖。

Class 的基本语法

简介

ES6 的类完全可以看作构造函数的另一种写法,类的所有方法都定义在类的 prototype 属性上,类的数据类型就是函数,类本身就指向构造函数。

class Point {
  constructor() {}
  toString() {}
}

typeof Point // function
Point === Point.prototype.constructor // true

// 等同于
Point.prorotype = {
  constructor() {},
  toString() {}
}

在类的实例上调用方法,其实就是调用原型上的方法。使用 Object.assign 方法可以方便向类添加多个方法。类的内部定义的方法都是不可枚举的(non-enumerable),这点与 ES5 表现不一致。

class Point {}
let p = new Point()
p.constructor === Point.prototype.constructor // true

Object.assign(Point.prototype, {
  toString()
})

constructor

constructor 方法默认返回实例对象(即 this),不过完全可以指定返回另外一个对象。实例的属性除非显式定义在其本身(即 this 对象)上,否则都是定义在原型上。

class Foo {
  constructor() {
    return Object.create(null)
  }
}
new Foo() instanceof Foo // false

class Point {
  constructor(x) {
    this.x = x
  }
  toString() {}
}
const p = new Point(1)
p.hasOwnProperty('x') // true
p.hasOwnProperty('toString') // false
p.__proto__.hasOwnProperty('toString') // true

proto 是浏览器厂商添加的私有属性,应避免使用,在生产环境中,可以使用 Object.getPrototypeOf 方法来获取实例对象的原型。

Class 表达式

Class 可以使用表达式形式定义:

const MyClass = class Me {
  getClassName() {
    return Me.name
  }
}

const inst = new MyClass()
inst.getClassName() // Me
Me.name // ReferenceError: Me is not defined

需要注意:上面定义的类的名字是 MyClass 而不是 Me,Me 只在 Class 的内部代码可用,指代当前类。如果 Class 内部没有用到 Me,则可以省略。同时,也可以写出立即执行 Class。

// 省略 Me
const MyClass = class {}

// 立即执行 Class
const p = new (class {})()

不存在变量提升

类不存在变量提升(hoist),这点与 ES5 完全不同。这与类的继承有关,因为要确保父类在子类之前定义,如果出现变量提升,则会出错。

// 确保父类在子类之前定义
const Foo = class {}
class Bar extends Foo {}

私有方法

利用 Symbol 值的唯一性将私有方法的名字命名为一个 Symbol 值,从而实现私有方法。

const bar = Symbol('bar')

class Point {
  foo() {
    this[bar]()
  }

  [bar]() {}
}

this 指向

类的方法内部如果含有 this,它将默认指向类的实例。但是,必须非常小心,一旦单独使用该方法,很可能会报错。

class Logger {
  printName() {
    this.print()
  }
  print() {
    console.log('Hello')
  }
}
const logger = new Logger()
const { printName } = logger
printName() // TypeError: Cannot read property 'print' of undefined

一种解决办法是在 constructor 里绑定 this。

class Logger {
  constructor {
    this.printName = this.printName.bind(this)
  }
}

更巧妙的方式是使用 Proxy,在获取方法时自动绑定 this。

function selfish(target) {
  const cache = new WeakMap()
  const handler = {
    get(target, key) {
      const value = Reflect.get(target, key)
      if (typeof value !== 'function') return value
      if (!cache.has(value)) cache.set(value, value.bind(target))
      return cache.get(value)
    }
  }
  return new Proxy(target, handler)
}
const logger = selfish(new Logger())

new.target

ES6 为 new 命令引入了 new.target 属性,返回 new 命令所作用的构造函数。

function Person(name) {
  if (new.target === Person) {
    this.name = name
  } else {
    throw new Error('必须使用 new 生成实例')
  }
}

需要注意:子类继承父类时 new.target 会返回子类。利用这个特点,可以写出不能独立独立使用而必须继承后才能使用的类。

class Shape {
  constructor() {
    if (new.target === Shape) {
      throw new Error('本类不能实例化')
    }
  }
}

class Rectangle extends Shape {
  constructor(length, width) {
    super()
  }
}

Class 的继承

简介

Class 可以通过 extends 关键字实现继承,子类必须在 constructor 方法中调用 super 方法,否则新建实例时会报错。这是因为子类没有自己的 this 对象,而是继承父类的 this 对象,然后对其进行加工。如果不调用 super 方法,子类就得不到 this 对象。

class Point {}
class ColorPoint extends Point {
  constructor() {}
}
const cp = new ColorPoint() // ReferenceError

ES5 的继承实质是先创造子类的实例对象 this,然后再将父类的方法添加到 this 上面(Parent.apply(this))。

ES6 的继承机制完全不同,实质是先创造父类的实例对象 this(所以必须先调用 super 方法),然后再用子类的构造函数修改 this。如果子类没有定义 constructor 方法,则会被默认添加。且只有调用 super 之后才能使用 this 关键字。

class ColorPoint extends Point {}

// 等同于
class ColorPoint extends Point {
  constructor(...args) {
    super(...args)
  }
}

Object.getPrototypeOf()

Object.getPrototypeOf 方法可以用来从子类上获取父类。因此,可以使用这个方法来判断一个类是否继承了另一个类。

Object.getPrototypeOf(ColorPoint) === Ponit // true

super 关键字

super 这个关键字既可以当作函数使用,也可以当作对象使用。在这两种情况下,它的用法完全不同。

第一种情况,super 作为函数调用时代表父类的构造函数。需要注意,super 虽然代表了父类的构造函数,但返回的是子类的实例,即 super 内部的 this 指向的是 ColorPoint,此时 super() 相当与 Point.prototype.constructor.call(this)。

class A {
  constructor() {
    console.log(new.target.name)
  }
}

class B extends A {
  constructor() {
    super()
  }
}

new A() // A
new B() // B

上面的代码中,new.target 指向当前正在执行的函数,在 super 函数执行时,它指向的是子类的构造函数,即 super() 内部的 this 指向的是 B。

第二种情况,super 作为对象时在普通方法中指向父类的原型对象,在静态方法中指向父类。需要注意,由于普通方法中 super 指向父类的原型对象,所以定义在父类实例上的方法或属性是无法通过 super 调用的。

class Parent {
  static myMethod(msg) {
    console.log('static', msg)
  }
  myMethod(msg) {
    console.log('instance', msg)
  }
}

class Child extends Parent {
  static myMethod(msg) {
    super.myMethod(msg)
  }
  myMethod(msg) {
    super.myMethod(msg)
  }
}

Child.myMethod(1) // static 1
const child = new Child()
child.myMethod(2) // instance 2

class A {
  constructor() {
    // 无法获得
    this.p = 2
  }
}
// 可以获得
A.prototype.p = 2

作为普通方法调用时,super 指向 A.prototype,所以 super.func() 相当于 A.prototype.func()。同时 super 会绑定子类的 this,super.func() 相当于 super.func.call(this)。

由于绑定子类的 this,因此如果通过 super 对某个属性赋值,这时 super 就是 this,赋值的属性会变成子类实例的属性。

class A {
  constructor() {
    this.x = 1
  }
}
class B extends A {
  constructor() {
    super()
    this.x = 2
    super.x = 3
    console.log(super.x) // undefined
    console.log(this.x) // 3
  }
}

上面的代码中,super.x 被赋值为 3,等同于对 this.x 赋值为 3。当读取 super.x 时,相当于读取的是 A.prototype.x,所以返回 undefined。

prototype 和 proto

在 ES5 中,每一个对象都有 __proto__ 属性,指向对应的构造函数的 prototype 属性。Class 作为构造函数的语法糖,同时有 prototype 属性和 __proto__ 属性,因此同时存在两条继承链。

  • 子类的 __proto__ 属性表示构造函数的继承,总是指向父类。

  • 子类的 prototype 属性的 __proto__ 属性表示方法的继承,总是指向父类的 prototype 属性。

class A {}
class B extends A {}
B.__proto__ === A // true
B.prototype.__proto__ === A.prototype // true

造成这样的结果是因为类的继承是按照下面的模式实现的:

// B 的实例继承 A 的实例
Object.setPrototypeOf(B.prototype, A.prototype)
// B 的实例继承 A 的静态属性
Object.setPrototypeOf(B, A)

其中 Object.setPrototypeOf 的实现如下:

Object.setPrototypeOf = function(obj, proto) {
  obj.__proto__ = proto
  return obj
}

所以上面的代码等同如下:

Object.setPrototypeOf(B.prototype, A.prototype)
// 等同于
B.prototype.__proto__ = A.prototype

Object.setPrototypeOf(B, A)
// 等同于
B.__proto__ = A

两条原型链理解如下:作为一个对象,子类(B)的原型(__proto__ 属性)是父类(A);作为一个构造函数,子类(B)的原型(prototype 属性)是父类的实例。

extends 的继承目标

下面讨论三种特殊的继承情况。

第一种特殊情况,子类继承 Object 类:

class A extends Object {}
A.__proto__ === Object // true
A.prototype.__proto__ === Object.prototype // true

这种情况下,A 其实就是构造函数 Object 的复制,A 的实例就是 Object 的实例。

第二种特殊情况,不存在任何继承:

class A {}
A.__proto__ === Function.prototype // true
A.prototype.__proto__ === Object.prototype // true

这种情况下,A 作为一个基类(即不存在任何继承)就是一个普通函数,所以直接继承 Function.prototype。但是,A 调用后返回一个空对象(即 Object 实例),所以 A.prototype.__proto__ 指向构造函数(Object)的 prototype 属性。

第三种特殊情况,子类继承 null:

class A extends null {}
A.__proto__ === Function.prototype // true
A.prototype.__proto__ === undefined // true

这与第二种情况非常像。A 也是一个普通函数,所以直接继承 Function. prototype。但是,A 调用后返回的对象不继承任何方法,所以它的 __proto__ 指向 Function.prototype。

实例的 __proto__

子类实例的 __proto__ 属性的 __proto__ 属性指向父类实例的 __proto__ 属性。也就是说,子类的原型的原型是父类的原型。

const p1 = new Point(2, 3)
const p2 = new ColorPoint(2, 3, 'red')
p2.__proto__ === p1.__proto__ // false
p2.__proto__.__proto__ === p1.__proto__ // true

Mixin 模式

Mixin 模式指的是将多个类的接口“混入”(mixin)另一个类,在 ES6 中的实现如下:

function mix(...mixins) {
  class Mix {}
  for (let mixin of mixins) {
    copyProperties(Mix, mixin)
    copyProperties(Mix.prototype, mixin.prototype)
  }
  return Mix
}
function copyProperties(target, source) {
  for (let key of Reflect.ownKeys(source)) {
    if (key !== 'constructor' && key !== 'prototype' && key !== 'name') {
      let desc = Object.getOwnPropertyDescriptor(source, key)
      Object.defineProperty(target, key, desc)
    }
  }
}

上面代码中的 mix 函数可以将多个对象合成为一个类。使用的时候,只要继承这个类即可。

class DistributedEdit extends mix(Loggable, Serializable) {
  // ...
}
上一页08 Async 函数下一页JavaScript 设计模式

最后更新于2年前

这有帮助吗?