Class类

2023-05-16

class类的基本写法

  • es6引入了class类的概念,可通过class关键字来定义类
  • 每个类都会有一个构造函数,即constructor()方法,用于创建和初始化class对象
  • 要注意,如果一个类没有指定constructor()方法则会默认添加,其中this关键字代表实例对象
class CustomData {
   constructor(x,y){
      this.x = x;
      this.y = y;
   }
   // 定义方法
   toString() { return '(' + this.x + ', ' + this.y + ')'; }
}
  • 使用时和构造函数一样,通过new命令生成对象实例
 let p = new CustomData(1, 2);
 console.log(p) // Point {x: 1, y: 2}
 console.log(p.toString())   // "(1, 2)"
  • 类的数据类型就是函数,类本身指向构造函数constructor
console.log(typeof CustomData) // function
console.log(CustomData === CustomData.prototype.constructor) // true
  • 类的所有方法都定义在prototype对象上,所有类的新方法可以添加在prototype对象上。
  • 其中Object.assign()方法可以很方便地一次向类添加多个方法
p.add = function (){ return this.x + 5 }
console.log(p.add()) // 6
===================================
Object.assign(CustomData.prototype, {
   add(){  return this.x + 5 },
   addSuffix(){ return this.x + 'Suffix' }
});
console.log(p.add()) // 6
console.log(p.addSuffix()) // 1Suffix
  • 类内部定义的所有方法都是不可枚举的
    Object.getOwnPropertyNames() 返回一个数组,元素为对象自身属性的所有名称,包括不可枚举值,它不考虑继承的属性
console.log(Object.keys(CustomData.prototype)) // []
console.log(Object.getOwnPropertyNames(CustomData.prototype)) // ["constructor", "toString"]
  • xy都是实例对象自身的属性 ( 定义在this对象上 ),所以CustomData.hasOwnProperty('x') // true
  • toString原型对象的属性 ( 定义在CustomData上 ),所以CustomData.hasOwnProperty('toString') // false

私有属性/私有方法———只能在类的内部访问的属性和方法,外部无法访问

  • 给class类添加私有属性,可在属性面前添加#,私有方法同理
class CustomData {
   #count = 'count' // 私有属性
   constructor(x,y) {
      this.x = x;
      this.y = y;
   }
   toString() { return this.#count + this.#handler(); } //可内部调用
   // 私有方法
   #handler() { return this.x }
}
let p = new CustomData(1,2)
console.log(p.toString()) // count1
console.log(p.#count)     // 报错
console.log(p.#handler)   // 报错
  • 另外,私有方法也可通过命名方式加以区别,比如通过在方法名前添加_下划线来定义此方法为私有方法,命名的方式只是为了使用时区分,本身在类的外部还是可以正常调用的
  • 因为类的所有方法都是对外可见的,或许我们可以直接将私有方法移出类,通过类内部调用私有方法,同时利用call()改变被调用的私有方法this指向
class CustomData {
   constructor(x,y) {
      this.x = x;
      this.y = y;
   }
   toString() {
     return handler.call(this,this.x,this.y)
   }
}
function handler(parameter1,parameter2) {
   return parameter1 + parameter2;
}
let p = new CustomData('参数1',"参数2")
console.log(p.toString()) // 参数1参数2

静态属性/静态方法———在方法面前添加static关键字表明此方法为静态方法

  • 在类中定义的所有方法都会被实例继承,但如果是静态属性/静态方法,则表示该属性/方法不会被实例继承,而是直接通过类来调用
export class CustomData {
	// 静态属性
	static prop = '静态属性';
   // 静态方法
   static toString() { return '我是一个静态方法'; }
}
console.log(CustomData.prop) // 我是一个静态方法  // 直接通过类来调用
console.log(CustomData.toString()) // 我是一个静态方法  // 直接通过类来调用
let p = new CustomData(1,2)
console.log(p.toString()) // [object Object] 不会被实例继承,故不可以通过实例对象调用
console.log(p.prop) // undefined
  • 注意:父类的静态方法是可以被子类继承的,不要和实例无法继承这一点混淆
  • 另外在子类中,可以通过super对象调用父类的静态方法
export class Custom_object extends CustomData{
	static toString_object(){ return super.toString() + ',子类'}
}
console.log(Custom_object.toString()) // 我是一个静态方法
console.log(Custom_object.toString_object()) // 我是一个静态方法,子类

class类的继承

  • class通过extends关键字实现继承,让子类继承父类的属性和方法
  • es6中的extends继承必须先调用super()方法,因为这一步会生成一个继承父类的this对象,不调用无法继承父类
  • 也是在子类的构造函数中,只有调用了super()方法后,才可以使用this关键字,否则会报错
  • 另一方面来讲调用super(),在新建子类实例时,父类的构造函数必定会先运行,即 “继承在前,实例在后”
class CustomData {
   constructor(x,y) {
      this.x = x;
      this.y = y;
      console.log('父类') // 1 先输出“父类”
   }
   toString() { return '(' + this.x + ', ' + this.y + ')'; }
}
class Custom_object extends CustomData{
   constructor() {
      super();
      console.log('子类') // 2 再输出“子类”
   }
}
let p = new Custom_object()
console.log(p) // 3最后Custom_object{x: undefined, y: undefined}
  • 继承父类的子类,通过super()继承父类,同时通过传参继承父类的属性
  • 在继承关系中,实例对象同时是父类和子类的实例
class CustomData {
   constructor(x,y) {
      this.x = x * 2 ;
      this.y = y;
   }
}
class Custom_object extends CustomData{
   constructor(x,y,color) {
      super(x,y);
      this.color = this.x + this.y + color
   }
}
let p = new Custom_object(1,2,'color')
console.log(p); // Custom_object {x: 1, y: 2, color: "4color"}
console.log(p instanceof Custom_object) // true
console.log(p instanceof CustomData) // true
  • 子类中,除了私有属性和私有方法,父类的所有属性和方法都会被子类继承,其中包括静态属性和静态方法

Object.getPrototypeOf()方法

  • 可从子类上获取父类,因此可通过此法来判断一个类是否继承了另一个类,类似instanceof作用
class CustomData {
   constructor(x,y,) {
      this.x = x ;
      this.y = y;
   }
   toString() { return '(' + this.x + ',' + this.y + ')' }
}
class Custom_object extends CustomData{
   constructor(x,y,color) {
      super();
      this.color = this.x + this.y + color
   }
}
console.log(Object.getPrototypeOf(Custom_object)) // ƒ CustomData(x, y) {}
console.log(Object.getPrototypeOf(Custom_object) === CustomData) // true
console.log(new Custom_object() instanceof CustomData) // true

super 关键字

  • super既可以当做函数使用,也可当做对象使用
  • 当作为函数使用时,代表父类的构造函数。且在子类的构造函数中必须执行一次super函数
  • 当作为对象时,在普通方法中,指向父类的原型对象
class CustomData {
   constructor(x,y) {
      this.x = x ;
      this.y = y;
   }
   toString() { return '(' + this.x + ',' + this.y + ')' }
}
class Custom_object extends CustomData{
   constructor(x,y) {
      super(x,y); // 函数时
      console.log(super.toString())
   }
}
let p = new Custom_object(1,2)

====================================================================

call、apply、bind的区别

callapplybind 每个函数都有这三个属性,它们的作用都是可以改变函数内部的this指向,且三个的第一个参数是this要指向的对象

  • callapply传参方式不一样,call可以枚举多个参数;apply只有两个参数,第二个参数为数组
    Function.call(this,parameter1,parameter2)
    Function.apply(this,[parameter1,parameter2])

  • bindcall使用方式一致,不过需要多一步手动调用
    let foo = Function.bind(this,[parameter1,parameter2])
    foo()

====================================================================

instanceof

  • 在js中判断一个变量的类型常用typeof,但如果是用引用类型,无论是什么类型的变量,都只会返回Object,为此引入了instanceof,用以验证引用类型为那种类型
let arr = []
let obj = {}
console.log(typeof arr) // Object
console.log(typeof obj) // Object
console.log(arr instanceof Array,arr instanceof Object) // true true
console.log(obj instanceof Object)  // true
  • 可以判断实例对象是否是类的实例
  • 在继承关系中判断实例对象是否是父类的实例
function Father(){}
function Son(){}
Son.prototype = new Father(); // 原型继承
let sonDate = new Son()
console.log(sonDate instanceof Son) // true 判断变量sonDate是否是Son的实例
console.log(sonDate instanceof Father) //true 在继承关系中判断实例对象是否是父类的实例

待续。。。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Class类 的相关文章

随机推荐

  • reduce()累计器理解与使用实例

    关于reduce reduce 语法 xff1a arr reduce callback accumulator currentValue index array initialValue arr reduce accumulator cu
  • {}与Object.create(null)

    var one 61 创建的对象带有 proto 下面有一些方法与属性 xff0c 这便是js的原型链继承 xff0c 继承了object的方法和属性 xff1b 故在遍历对象时 xff0c 会遍历原型链上的属性 xff0c 带来性能上的损
  • uni-app 全局变量机制

    getApp globalData 全局变量机制 在App vue中 export default globalData text 39 text 39 在App vue中调用 this globalData text在onLaunch生命
  • vue3动态注册路由

    在vue cil2中 xff0c 我们可以通过webpack中require context这个api实现工程自动化 xff0c 而在vue cil3里vite替代了webpack xff0c 节省了webpack冗长的打包时间的同时我们也
  • try{}catch(res){}、throw(exception)、new Error()

    1 try catch res try 中的代码出现错误异常时 xff0c 系统会将异常信息封装到error对象中 xff0c 传递给catch res xff0c 包含res message res name等 EvalError eva
  • new Map()

    1 new Map let data 61 new Map data set key value 添加一个新建元素到映射 Map 1 key 61 gt value data get key 返回映射中的指定元素 data has key
  • Proxy代理

    Proxy用于修改某些操作的默认行为 xff0c 等同于在语言层面做出修改 xff0c 所以属于一种 元编程 语法 xff1a let proxy 61 new Proxy target handler target 所要拦截的目标对象ha
  • Jmeter性能测试(7)--定时器

    jmeter xff08 七 xff09 定时器 jmeter提供了很多元件 xff0c 帮助我们更好的完成各种场景的性能测试 xff0c 其中 xff0c 定时器 xff08 timer xff09 是很重要的一个元件 xff0c 最新的
  • oninput完美限制输入正整数

    oninput完美限制输入非0正整数 注意vue中需要 64 input进行绑定 方法一 64 input 61 34 if this value length 61 61 1 this value 61 this value replac
  • 行内存放数据属性data-id

    data 61 39 data 39 为行内存放数据的属性 xff0c 可通过事件源中的currentTarget dataset获取data 存放的值 另外css可通过 data 放置的标签名 data 61 39 data 39 设置
  • js常用封装方法

    span class token comment 生成随机数 64 length 指定长度 return 随机数 span span class token keyword export span function span class t
  • 计数器组件

    涉及事件 64 longpress 长按时触发 xff0c 64 touchend 手指从屏幕上离开时触发 1 计数器为文本标签的子组件 lt template gt lt view class 61 34 counter box 34 g
  • rich-text 富文本

    rich text 富文本 普通的text文件不能显示格式 xff0c 富文本格式rtf文件可以显示出很多格式信息 xff0c 比如可以在一个文本包含不同颜色 不同字号的文本 官方 lt rich text nodes 61 34 cont
  • uni-app实现全局组件注册

    uni app 全局注册组件三种方式 1 传统vue组件需要创建 引用 组成三个步骤 2 在page json中对应page设置 34 globalStyle 34 34 autoscan 34 true 和pages同级 3 HBuild
  • Vue--混入(Mixin)

    Vue 混入 Mixin 当不同组件有相同功能时 xff0c 不必重复定义属性和方法 xff0c 可使用vue中的混入 Mixin 来分发 Vue 组件中的可复用功能 一个 mixin 对象可以包含任意组件选项 xff0c 即data me
  • uni-app--tabs切换swiper

    父组件 span class token operator lt span template span class token operator gt span span class token operator lt span view
  • 关于移动端 html5诸多事件

    1 点击事件 64 click与 64 tap的区别 xff1a 64 click 在web手机端上点击 xff0c 有300ms延迟再被触发 64 tap具有事件穿透特点 而 64 click没有 事件冒泡 xff1a 当父元素有点击事件
  • vuex状态管理

    vue 1 下载vuex依赖 2 创建store目录store js xff0c 然后在js中引入 span class token keyword import span span class token module Vue span
  • Jmeter性能测试(8)--断言

    jmeter xff08 8 xff09 断言 jmeter中有个元件叫做断言 xff08 Assertion xff09 xff0c 它的作用和loadrunner中的检查点类似 xff1b 用于检查测试中得到的响应数据等是否符合预期 x
  • Class类

    class类的基本写法 es6引入了class类的概念 xff0c 可通过class关键字来定义类每个类都会有一个构造函数 xff0c 即constructor 方法 xff0c 用于创建和初始化class对象要注意 xff0c 如果一个类