ES6面向对象

Wesley13
• 阅读 479

ES6面向对象

js中的面向对象

function User(name, age) {
    this.name = name; // 定义属性
    this.age = age; 
}

// 通过原型添加方法
User.prototype.showName = function() {
    console.log(this.name);
};

let user = new User('rose', 20); // 实例化
user.showName(); // 调用方法

可以看到js的类和构造函数是没什么区别

ES6有了个关键字:Class,来定义类,和java差不多

class User {
    constructor(name, age) { // 构造器
        this.name = name;
        this.age = age;
    }
    // 定义类方法
    showName() {
        console.log(this.name);
    }
}
let user = new User('rose', 21);
user.showName();

这个用了ES6写法的class 和 上面用构造函数定义的类,输出结果都一样。

ES6的class 有了专门的构造器 constructor,构造器和类分开了,定义方法:不需要原型来定义了,直接再类里面定义方法。

ES6定义类的写法

// 匿名类
let Example1 = class {
    constructor(a) {
        this.a = a;
        console.log(a);
    }
}
let ex1 = new Example1(1);
// 命名类
let Example2 = class Example2 {
    constructor(a) {
        this.a = a;
        console.log(a);
    }
}
let ex2 = new Example2(2);

// 类声明
class Example {
    // class主体内容
    constructor(a, b) {
        this.a = a;
        this.b = b;
    }
}

注意点:

  1、不可重复声明

  2、类定义不会被提升,这意味着,必须在访问前对类进行定义,否则就会报错。类中方法不需要 function 关键字。

  3、方法间不能加分号。

  4、class 的实例化必须通过 new 关键字。

面向对象的三大特点:封装、继承、多态。ES6有了class可以封装了方法,也有继承。

js的继承

function User(name, age) {
    this.name = name;
    this.age = age;
}
User.prototype.showName = function() {
    console.log(this.name);

}
let user = new User('rose', 21);
//user.showName();

function VipUser(name, age, level) {
    User.call(this, name, age); // 通过call调用User类
    
    this.level = level; // vipUser对象的属性 
}

VipUser.prototype = new User(); // 通过原型实现继承
VipUser.prototype.constructor = VipUser;

VipUser.prototype.showLevel = function() {
    console.log(this.level);
}
let vip = new VipUser('rose', 20 , 10);
vip.showName(); // 使用父类的方法
vip.showLevel(); // 使用自己的方法

ES6的继承:关键字:extends 继承、super 超级

class User {
    constructor(name, age) { // 构造函数
        this.name = name;
        this.age = age;
    }
    // 定义类方法
    showName() {
        console.log(this.name);
    }
}

class VipUser extends User {
    constructor(name, age, level) {
        super(name, age); // 父类
        this.level = level; // 自己的属性
    }
    
    showLevel() {
        console.log(this.level);
    }
}

let vip = new VipUser('rose', 22, 2); // 实例化vipUser类
vip.showName();
vip.showLevel(); // 调用方法

Class 中的this指向

class User {
    constructor(name, age) {
        //constructor 里面的this
        this.name = name;
        this.age = age;
    }
    
    showName() {
        // 方法里面的this
        console.log(this.name);
    }
}

let user = new User('rose', 10);

1、constructor  构造器里面的this是指向创建的实例对象

let that;
class User {
    constructor(name, age) {
        //constructor 里面的this
        this.name = name;
        this.age = age;
        that = this;
        console.log(this);
    }
    
    showName() {
        // 方法里面的this
        console.log(this.name);
    }
}

// 每次实例化都会调用constructor构造器里面的代码
let user = new User('rose', 10); // User {name: "rose", age: 10}

console.log(that === user); // 构造器的this和实例的对象对比:true

2、方法里面的this

let that;
class User {
    constructor(name, age) {
        //constructor 里面的this
        this.name = name;
        this.age = age;
    }
    
    showName() {
        // 方法里面的this
        console.log(this.name);
    }
    
    showThis() {
        console.log(this); // 方法里面的this
        // 一般方法里面的this都是指向 谁 调用这个方法就指向 谁
        that = this;
    }
}

let user = new User('rose', 10);
user.showThis();
console.log(that === user); // true

<button id='bottom'>点我</button>
<script>
    let that;
    class User {
        constructor(name, age) {
            //constructor 里面的this
            this.name = name;
            this.age = age;
            this.btn = document.getElementById('bottom');
            this.btn.onclick = this.userAge;
        }
        
        showName() {
            // 方法里面的this
            console.log(this.name);
        }
        
        showThis() {
            console.log(this); // 方法里面的this
            // 一般方法里面的this都是指向 谁 调用这个方法就指向 谁
            that = this;
        }
            
        userAge() {
                // 这个函数里面的this:指向了btn按钮,因为是按钮调用了这个函数,
                console.log(this.age);
            }
    }
    
    let user = new User('rose', 10);
    // 点击页面按钮输出 undefined ,因为按钮对象没有定义age属性
</script>

要点击按钮输出我实例的User的age

<button id='bottom'>点我</button>
<script>
    let that;
    class User {
        constructor(name, age) {
            //constructor 里面的this
            this.name = name;
            this.age = age;
            this.btn = document.getElementById('bottom');
            this.btn.onclick = this.userAge;
            
            that = this;
        }
        
        showName() {
            // 方法里面的this
            console.log(this.name);
        }
        
        showThis() {
            console.log(this); // 方法里面的this
            // 一般方法里面的this都是指向 谁 调用这个方法就指向 谁
        }
            
        userAge() {
                // that 存储了构造器里面的this
                console.log(that.age);
            }
    }
    
    let user = new User('rose', 10);
    // 点击页面按钮输出 10 
</script>

通过全局变量that,that === 实例化的对象。

点赞
收藏
评论区
推荐文章
ZY ZY
2年前
js继承的几种方式
1.原型链继承原型链继承:想要继承,就必须要提供父类(继承谁,提供继承的属性)//父级functionPerson(name)//给构造函数添加参数this.namename;this.age10;this.sumfunction()console.log(this.name)//原
从一个 10 年程序员的角度告诉你:搞懂 Java 面向对象有多容易?
前言:1)java面向对象语言,面向过程围绕过程(解决问题步骤),面向对象围绕实体(名词,特性(属性),行为(动作、方法))。它们设计思想区别在于关心核心不同的。主流都是面向对象的。实际开发,先按面向对象思想进行设计,具体实现时面向过程(人习惯)2)java怎么支持面向对象呢?a.万物皆对象,所有的类都是Object子类b.java中支
Wesley13 Wesley13
2年前
JS实现继承的几种方式
前言JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一。那么如何在JS中实现继承呢?让我们拭目以待。JS继承的实现方式既然要实现继承,那么首先我们得有一个父类,代码如下://定义一个动物类functionAnimal(name){//属性this.name
Wesley13 Wesley13
2年前
JS必知的6种继承方式
JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一。那么如何在JS中实现继承呢?让我们拭目以待JS继承的实现方式既然要实现继承,那么首先我们得有一个父类,代码如下:// 父类function Person(name) { // 给构造函数添加了参数  this.name  name;
Stella981 Stella981
2年前
Javascript 是如何体现继承的 ?
js继承的概念js里常用的如下两种继承方式:原型链继承(对象间的继承)类式继承(构造函数间的继承) 由于js不像java那样是真正面向对象的语言,js是基于对象的,它没有类的概念。所以,要想实现继承,可以用js的原型prototype机制或者用apply和call方法去实现在面向对象的语言中,我们使用类来创建一个自定义对象
Stella981 Stella981
2年前
JavaScript面向对象编程的15种设计模式
在程序设计中有很多实用的设计模式,而其中大部分语言的实现都是基于“类”。在JavaScript中并没有类这种概念,面向对象编程不是基于类,而是基于原型去面向对象编程,JS中的函数属于一等对象,而基于JS中闭包与弱类型等特性,在实现一些设计模式的方式上与众不同。ps:本文之讲述面向对象编程的设计模式策略,JavaScript原型的基础请参考阮一峰面向
Wesley13 Wesley13
2年前
Java面试参考指南(一)
Java面向对象相关概念Java是一种基于面向对象概念的编程语言,使用高度抽象化来解决现实世界的问题。    面向对象的方法将现实世界中的对象进行概念化,以便于在应用之间进行重用。例如:椅子、风扇、狗和电脑等。Java里的类(Class)是一个蓝图、模板,或者称之为原型,它定义了同一类事物的相同属性和行为。实例(Instan
Wesley13 Wesley13
2年前
JAVA面向对象
一、java面向对象的特征?答:java面向对象的特征:封装、继承、多态、抽象。如果问java面向对象的三大特征是什么?那就是封装、继承、多态,但说到面向对象,还有一个很主要的特征就是抽象。1、封装a)铺垫:Java把真实世界中某些具有共同特征的实体抽象成Java中的类,类的实例就是一个对象,而对象和真实世界的实体是一一
Wesley13 Wesley13
2年前
Java面向对象
1\.方法    方法就是对java代码功能的封装。1.1方法定义的格式\访问修饰符\\关键字\static\\返回值类型方法名(\参数列表\){}forexample:    public static int add(int a,int