定义

类是构造函数、原型链的语法糖。

定义类有两种方式

class Student {
}
var Student = class {
}

某些浏览器可能无法解析es6及以上的语法,这时候需要通过babel将代码解析成浏览器可识别的语法,定义类的语法通过babel编译之后就是通过function定义的构造函数。

类和构造函数是一样的,通过new关键字创建,具有prototype属性

class Student{}
var student = new Student()
console.log(Student.prototype)
console.log(Student.prototype.constructor)
console.log(student.__proto__ === Student.prototype)
console.log(student instanceof Student)
console.log(typeof Student)

执行结果如下

类的方法

构造方法

通过constructor来定义类的构造方法,通过new关键字来创建类的实例时会执行构造方法中的代码

class Student {
  constructor(name, age) {
    this.name = name
    this.age = age
  }
}
var student = new Student('alice', 18)
console.log(student)

执行结果如下,创建了一个Student的实例对象

babel解析结果如下

实例方法

实例方法就是挂载在类(构造函数)原型上的方法,可以供所有的实例对象使用,不会在每个实例对象上保存一份

class Student {
  constructor(name, age) {
    this.name = name
    this.age = age
  }
  studying() {
    console.log(`${this.name} likes studing~`)
  }
}
var student = new Student('kiki', 16)
console.log(student)
student.studying()

执行结果如下

访问器方法

访问器方法可以用于获取/修改类中的属性

class Student {
  constructor(){
    this.mainSubject = 'Chinese'
  }
  get subject(){
    console.log('获取主修课')
    return this.mainSubject
  }
  set subject(value){
    console.log('修改主修课')
    this.mainSubject = value
  }
}
var student = new Student()
console.log(student)
student.mainSubject = 'Math'
console.log(student)

执行结果如下

静态方法

定义在类(构造函数)上,且仅供类(构造函数)自身可使用

class Student {
  static showInfo(){
    console.log('我是一个Student类')
  }
}
Student.showInfo()

执行结果如下

继承

类中实现继承要比构造函数中更为简单,通过extends关键字就可以实现两个类的继承关系。

class Person{
  eating(){
    console.log('person eating')
  }
}
class Student extends Person{
}
var student = new Student()
console.log(student)
student.eating()

执行结果如下

如果要共享构造方法中的数据,则需要通过super来实现

class Person{
  constructor(name, age){
    this.name = name
    this.age = age
  }
  eating(){
    console.log('person eating')
  }
} class Student extends Person{
  constructor(name, age, stuNo){
    super(name, age)
    this.stuNo = stuNo
  }
  eating(){
    super.eating()
    console.log('student eating')
  }
} var student = new Student('kiki', 16, 1)
console.log(student)
student.eating()

执行结果如下

继承内置类

当我们需要对javascript内置的函数做一些扩充的时候,可以继承自内置的函数。比如对数组进行补充,新增一个返回数组中第一个元素的方法。

class iArray extends Array {
  firstItem(){
    return this[0]
  }
}
let arr = new iArray(1, 2, 3)
console.log(arr)
console.log(arr.firstItem())

执行结果如下

混入

javascript中只能单继承,不支持多个父类,当子类希望获取多个父类的属性和方法时,可以自定义mixin的方式来实现继承关系

function mixinRunner(BaseClass) {
  return class extends BaseClass {
    running() {
      console.log('running')
    }
  }
}
function mixinEater(BaseClass){
  return class extends BaseClass {
    eating() {
      console.log('eating')
    }
  }
}
class Person {
 
}
const Student = mixinEater(mixinRunner(Person))
const student = new Student()
student.running()
student.eating()

执行结果如下

多态

不同的数据类型操作执行同一个操作时,表现出来的行为不一致,就称为多态。

function calcArea(foo) {
  console.log(foo.getArea())
} var circle = {
  radius: 6,
  getArea() {
    return this.radius * 3.14
  }
} function Person() {
  this.getArea = function(){
    return 20
  }
} calcArea(circle)
calcArea(new Person())

执行结果如下

以上执行两次calcArea函数,传入的参数分别为普通对象和实例对象,执行他们各自的getArea方法,最后获取的结果也不一样

以上就是ES6之类(class)使用的具体介绍,关于js高级,还有很多需要开发者掌握的地方,可以看看我写的其他博文,持续更新中~

ES6之清楚明白的使用类(class)的更多相关文章

  1. 【ES6】更易于继承的类语法

    和其它面向对象编程语言一样,ES6 正式定义了 class 类以及 extend 继承语法糖,并且支持静态.派生.抽象.迭代.单例等,而且根据 ES6 的新特性衍生出很多有趣的用法. 一.类的基本定义 ...

  2. 《深入理解ES6》笔记—— JavaScript中的类class(9)

    ES5中的近类结构 ES5以及之前的版本,没有类的概念,但是聪明的JavaScript开发者,为了实现面向对象,创建了特殊的近类结构. ES5中创建类的方法:新建一个构造函数,定义一个方法并且赋值给构 ...

  3. 用es6的class关键字定义一个类

    es6新增class关键字使用方法详解. 通过class关键字,可以定义类.基本上,ES6的class可以看作只是一个语法糖,它的绝大部分功能,ES5都可以做到,新的class写法只是让对象原型的写法 ...

  4. Es6对象的扩展和Class类的基础知识笔记

    /*---------------------对象的扩展---------------------*/ //属性简写 ,属性名为变量名, 属性值为变量的值 export default functio ...

  5. ES6快速入门(三)类与模块

    类与模块 一.类 一)类的声明 class Person { constructor(name) { this.name = name; } sayName() { console.log(this. ...

  6. ES系列五、ES6.3常用api之搜索类api

    1.搜索api 1.1.routing:路由 执行搜索时,它将广播到所有索引/索引分片(副本之间的循环).可以通过提供routing参数来控制将搜索哪些分片.例如,在索引book时,路由值可以是nam ...

  7. 使用ES6 Class封装的IndexDB 操作类,并实现模糊搜索

     封装如下: indexDBOperate.js export class IndexDBOperate { db = null // 数据库实例 databaseName = null // 数据库 ...

  8. 深入解析ES6 更易于继承的类语法的使用

    和其它面向对象编程语言一样,ES6 正式定义了 class 类以及 extend 继承语法糖,并且支持静态.派生.抽象.迭代.单例等,而且根据 ES6 的新特性衍生出很多有趣的用法. 一.类的基本定义 ...

  9. ES6中的类继承和ES5中的继承模式详解

    1.ES5中的继承模式 我们先看ES5中的继承. 既然要实现继承,首先我们得要有一个父类. Animal.prototype.eat = function(food) { console.log(th ...

  10. es6入门5--class类的基本用法

    在ES6之前,准确来说JavaScript语言并无类的概念,却有模拟类的做法.相比在类似java这类传统面向对象语言中通过类来生成实例,js则通过构造函数模拟类来生成实例. 这是因为在JS设计初期,作 ...

随机推荐

  1. python介绍、32位与64位系统的区别、python安装、pip管理安装包

    一.python的介绍 * python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为 ...

  2. VueUse 是怎么封装Vue3 Provide/Inject 的?

    Provide/Inject Provide 和 Inject 可以解决 Prop 逐级透传问题.注入值类型不会使注入保持响应性,但注入一个响应式对象,仍然有响应式的效果. Provide 的问题是无 ...

  3. Weblogic11g安装部署-winserver篇

    目录 一.安装weblogic11g 1.1找到下载好的weblogic11g 1.2打开安装程序wls1033_oepe111150_win32.exe,并完成初始化如下图 1.3点击下一步并选择安 ...

  4. 2023-04-19:给定一个非负数组arr 任何两个数差值的绝对值,如果arr中没有,都要加入到arr里 然后新的arr继续,任何两个数差值的绝对值,如果arr中没有,都要加入到arr里 一直到ar

    2023-04-19:给定一个非负数组arr 任何两个数差值的绝对值,如果arr中没有,都要加入到arr里 然后新的arr继续,任何两个数差值的绝对值,如果arr中没有,都要加入到arr里 一直到ar ...

  5. 2022-03-26:给定一个无向图, 从任何一个点x出发,比如有一条路径: x -> a -> b -> c -> y, 这条路径上有5个点并且5个点都不一样的话,我们说(x,a,b,c,y)是一条

    2022-03-26:给定一个无向图, 从任何一个点x出发,比如有一条路径: x -> a -> b -> c -> y, 这条路径上有5个点并且5个点都不一样的话,我们说(x ...

  6. Winform 遮罩懒人处理法

    前言 之前有个项目需要执行一个略微耗时的操作大概五六七八九十秒这样子,这个时候程序不能做其他操作,只能等待操作完成.为了提升一丝使用体验同时让Winform程序看上去高级一点,就想到加一个遮罩层(Ma ...

  7. {"status":-1,"statusText":"ERR_CONNECT_FAILED"}

    今日使用weex 的stream 遇到一个极坑,也极傻的问题 一.steam.fetch 下面是我使用steam.fetch调用后台接口都截图 二.页面测试 奇怪的是,我借同事是手机来进行测试,有一个 ...

  8. web自动化09-frame切换、多窗口切换

    frame切换 1.html代码: <frameset cols="25%,50%,25%"> <frame src="a.htm"> ...

  9. 2023-06-11:redis中,如何在100个亿URL中快速判断某URL是否存在?

    2023-06-11:redis中,如何在100个亿URL中快速判断某URL是否存在? 答案2023-06-11: 传统数据结构的不足 当然有人会想,我直接将网页URL存入数据库进行查找不就好了,或者 ...

  10. JUC同步锁原理源码解析四----Semaphore

    JUC同步锁原理源码解析四----Semaphore Semaphore 1.Semaphore的来源 A counting semaphore. Conceptually, a semaphore ...