ES6 —— 类和对象

简介: ES6 —— 类和对象

一、创建类和生成实例

通过 class 关键字创建,类名首字符大写。

类里面有 constructor 函数,可以接受传递过来的参数,同时返回实例对象。

constructor 函数 只要 new 生成实例时,就会自动调用这个函数,如果不写这个函数,类也会自动生成这个函数。

生成实例 new 不能省略。

  // 1.创建类 class
    class Star {
        constructor(name, age){
            this.name = name
            this.age = age
        }
        // 在类中添加共有的方法
        sing(song){
            console.log(this.name + ' can sing ' + song);
        }
    }
    // 2.利用类创建对象 new
    let a = new Star('毛不易', 20)
    let b = new Star('不易', 22)
    console.log(a);
    console.log(b);
    a.sing('无名的人')
    b.sing('一荤一素')

3b0964c2b6c442b1addd224cd41a0a5f.png

二、类的继承

  1. 子类继承父类的 money 函数
  // 1.类的继承
    class Father {
        constructor(){
        }
        money() {
            console.log(100);
        }
    }
    class Son extends Father {
    }
    let son = new Son()
    son.money() //100
  1. super 关键字用于访问和调用对象父类上的函数。可以调用父类的构造函数,也可以调用父类的普通函数。

super 关键字调用父类中的 构造函数

  // 1.类的继承
    class Father {
        constructor(x, y){
            this.x = x
            this.y = y
        }
        sum() {
            console.log(this.x + this.y);
        }
    }
    class Son extends Father {
        constructor(x, y){
            super(x, y) //调用了父类中的构造函数
        }
    }
    let son = new Son(1, 2)
    son.sum() //3
  1. 继承中的属性或者方法查找原则:就近原则
  1. 继承中,如果实例化子类输出一个方法,先看子类有没有这个方法,如果有就先执行子类的。
  2. 继承中,如果子类里面没有,就去查找父类有没有这个方法,如果有,就执行父类的这个方法。

super 关键字调用父类的 普通函数

  class Father {
        say() {
            return '我是爸爸'
        }
    }
    class Son extends Father{
        say() {
            console.log('我是儿子');
        }
    }
    let son = new Son()
    son.say() //我是儿子


  class Father {
        say() {
            return '我是爸爸'
        }
    }
    class Son extends Father{
        say() {
            console.log(super.say() + '的儿子'); //super.say() 调用父类中的普通函数
        }
    }
    let son = new Son()
    son.say() //我是爸爸的儿子
  1. 子类在构造函数中使用 super,必须放到 this 前面(必须先调用父类的构造方法,再使用子类构造方法)
  //父类有加法方法
    class Father {
        constructor(x, y) {
            this.x = x
            this.y = y
        }
        sum() {
            console.log(this.x + this.y);
        }
    }
    //子类继承父类加法方法 同时扩展减法方法
    class Son extends Father{
        constructor(x, y){
            //调用父类的构造函数 (super 必须在子类 this 之前调用)
            super(x, y)
            this.x = x
            this.y = y
        }
        subtract() {
            console.log(this.x - this.y);
        }
    }
    let son = new Son(5, 3)
    son.subtract() //2
    son.sum() //8
  1. 类里面的共有属性和方法一定要加 this 使用。
  2. constructor 里面的 this 指向实例对象,方法里面的 this 指向这个方法的调用者。
  let that;
    let _that;
    class Star{
        constructor(uname){
            // constructor 里面的 this 指向的是 创建的对象实例
            that = this
            console.log(this); //Star {}
            this.uname = uname
            this.btn = document.querySelector('button')
            this.btn.onclick = this.sing
        }
        sing(){
            // 这个sing方法里面的this 指向的是 btn 这个按钮 因为按钮要调用这个函数
            console.log(this); //<button></button>
            console.log(that.uname); //毛不易 that 里面存储的是constructor里面的this
        }
        dance(){
            // 这个dance里面的 this 指向的是实例对象a 因为 a 调用了这个函数
            _that = this
            console.log(this); //Star {uname: '毛不易', btn: button}
        }
    }
    let a = new Star('毛不易')
    console.log(that === a); //true
    a.dance() 
    console.log(_that === a); //true

be0d2b4383e9485b97ee904f3c643edc.png

相关文章
|
7月前
ES6系列笔记-面向对象/继承
ES6系列笔记-面向对象/继承
21 1
|
7月前
ES5的继承和ES6的继承有什么区别
ES5的继承和ES6的继承有什么区别
33 0
|
10月前
ES5 / ES6 的继承除了写法以外还有什么区别
ES5 / ES6 的继承除了写法以外还有什么区别
|
11月前
|
JavaScript
面试题-TS(四):如何在 TypeScript 中使用类和继承?
在TypeScript中,类是一种重要的概念,它允许我们使用面向对象的编程风格来组织和管理代码。类提供了一种模板,用于创建具有相同属性和行为的对象。通过继承,我们可以创建类之间的层次结构,实现代码的重用和扩展。
|
12月前
ES6 —— 继承
ES6 —— 继承
|
缓存 JSON JavaScript
这一次该明白 ECMAScript Module 与 CommonJS 的异同点是什么了吧?
这一次该明白 ECMAScript Module 与 CommonJS 的异同点是什么了吧?
74 0
ES6 从入门到精通 # 22:类的继承
ES6 从入门到精通 # 22:类的继承
44 0
ES6 从入门到精通 # 22:类的继承
|
JavaScript 前端开发
【ES6】ES6编程规范 编程风格
【ES6】ES6编程规范 编程风格
|
JavaScript
ts(typescript)面向对象之类的继承
super当作属性使用的时候, super 指向的是父类的原型,因此super无法拿到父类的实例属性,只能拿到父类的public和protected的方法,super 如果当方法使用的话,只能在字类的构造函数中并且是第一行使用,需要使用super()来实例化父类里面的属性,确保字类可以获取父类的成员。
ts(typescript)面向对象之类的继承
|
JavaScript
ts(typescript)面向对象之静态成员
在js中,构造函数也是函数,只是特殊的地方在于,构造函数相对于普通函数来说。使用new 的方式来创建,并且构造函数里面是有this的。这个this 的指向是当前构造函数的对象。而普通的函数的this的指向是指向全局window的。
ts(typescript)面向对象之静态成员

热门文章

最新文章