第三十二节: ES6 Class类

1. ES6 Class 类

ES6 提供了更接近传统语言的写法,引入了 Class(类)这个概念,作为对象的模板。通过class关键字,可以定义类。

ES6 的class可以看作只是一个语法糖,它的绝大部分功能,ES5 都可以做到,新的class写法只是让对象原型的写法更加清晰、更像面向对象编程的语法而已。


1.1 . 复习构造函数

之前用函数模拟类

function Person(name,age){
    this.name = name;
    this.age = age; 
}
Person.prototype.showName = function(){
    return `我的名字是:${this.name}`;
}
Person.prototype.showAge = function(){
    return `我的名字是:${this.age}`;
}

let p1 = new Person("wuwei",18);
console.log(p1.showName());
console.log(p1.showAge());

但是之前讲了一个合并对象的,那么我们可以将新增的方法放在一个对象里合并到原型上

function Person(name,age){
    this.name = name;
    this.age = age; 
}
Object.assign(Person.prototype,{
    showName(){
        return `我的名字是:${this.name}`;
    },
    showAge(){
        return `我的名字是:${this.age}`;
    }
})

let p1 = new Person("wuwei",18);
console.log(p1.showName());
console.log(p1.showAge());


1.2 ES 6 变形的类Class
// 这是类 不是json 所以里面不需要加逗号
class Person{
    // 这是一个构造方法(函数),只要new Person() 这个构造函数会自动执行
    constructor(name,age){ 
        console.log(`构造函数执行了,name:${name},age:${age}`) 
    }
    
    //以下是构造函数原型上的方法
    
    showName(){
        return `名字:${this.name}`;
    }    // 不需要加逗号,加逗号就报错
    
    showAge(){
        return `年龄:${this.age}`;
    }
}


// 这个Person也是要给函数,typeof Person  打印function
let p1 = new Person("wuwei",18);
console.log(p1.showName());
console.log(p1.showAge());

表达式的类(不推荐)

var Person = class {
    constructor(){
        this.name = "wuwei";
    }
    showName(){
        return `名字:${this.name}`
    }
}


2. constructor

constructor方法是类的默认方法,通过new命令生成对象实例时,自动调用该方法。一个类必须有constructor方法,如果没有显式定义,一个空的constructor方法会被默认添加。

class Fn {
}

// 等同于
class Fn {
  constructor() {}
}

constructor方法默认返回实例对象(即this),完全可以指定返回另外一个对象。

class Foo {
  constructor() {
    return Object.create(null);
  }
}

new Foo() instanceof Foo
// false
//constructor函数返回一个全新的对象,结果导致实例对象不是Foo类的实例。


3. 类必须使用new调用

类必须使用new调用,否则会报错。这是它跟普通构造函数的一个主要区别,后者不用new也可以执行。

class Foo {
  constructor() {
    return Object.create(null);
  }
}

Foo()
// TypeError: Class constructor Foo cannot be invoked without 'new'


4. 方法名字可以是变量,

如果要是用变量,那么就要使用[]的表达式

var aaa = 'wu';
class Person{
    constructor(name,age){ 
        this.name = name;
        this.age = age;
    }
    [aaa](){
        return `此时属性名是一个变量,就是wu`
    }
}

// console.log(typeof Person)
let p1 = new Person("wuwei",18);
console.log(p1.wu());   // 此时不能通过aaa调用函数,aaa是变量,通过wu调用,可以通过[aaa]调用 p1[aaa]();

这里就可以发现属性名可以为表达式

var aa = 'wu';
var bb = 'wei';

var obj = {
    [aa + bb]: "无为"
}


5. ES6 class类没有提升和预解析的功能

也就是说如果写法会报错

var p1 = new Person()
console.log(p1);

class Person{
    constructor(){
        this.name = "aaa"
    }
}

但是以前通过ES5 构造函数模拟类是可以变量提升的,因为本身就是函数

// 这种写法构造函数会提升,正常打印一个对象
var p1 = new Person()
console.log(p1);
function Person(){
    this.name = "aaa"
}


6. get set

取值函数 get 存值函数 set

class Person{
    constructor(name,age){ 
        this.name = name;
        this.age = age;
    }
    get aa(){
        console.log(`成功获取a的值`)
        return this.age;
    }
    set aa(val){
        console.log(`成功设置a的值为${val}`);
        this.age = val;
    }
}

var p1 = new Person('wuwei',18)


7.静态方法

类相当于实例的原型,所有在类中定义的方法,都会被实例继承。

如果在一个方法前,加上static关键字,就表示该方法不会被实例继承,而是直接通过类来调用,这就称为“静态方法”。

ES6 明确规定,Class 内部只有静态方法,没有静态属性。

class Person{
    constructor(name,age){ 
        this.name = name;
        this.age = age;
    }
    showName(){
        return `这是通过构造对象调用方法`;
    }
    static aaa(){
        return `这是静态方法,是通过类来调用的方法`;
    }
}

var p1 = new Person('wuwei',18)
console.log(p1.showName);
console.log(Person.aaa());


8. 继承

子类继承父类

8.1 之前的子类继承父类
// 父类
function Person(name){
    this.name = name;
}
Person.prototype.showName = function(){
    return `名字是:${this.name}`;
}

// 子类
function Student(name,skill){
    Person.call(this,name);   // 继承父类的属性
    this.skill = skill;
}
Student.prototype = new Person; // 继承父类的方法


var stu = new Student("wuwei","逃课");
console.log(stu.name);
console.log(stu.showName())


8.2 ES6 类的继承 extends
class Student extends Person{}

这种继承 没法添加自己扩展的属性和方法,添加就报错

通过super()扩展自己的属性和方法

// 父类
class Person{
    constructor(name){
        this.name = name;
    }
    showName(){
        console.log('父类的showName方法');
        return `名字:${this.name}`;
    }
}

// 子类
class Student extends Person{
    constructor(name,skill){
        super(name); // 先执行父类的属性,继承后在设置自己的属性
        this.skill = skill;
    }
    // 继承父类的方法同时扩展内容
    showName(){
        super.showName(); // 先执行父类的showName方法
        // 在设置自己的showName方法
        console.log('执行了子类里面的showName');
    }

    // 设置自己的方法
    showSkill(){
        return `技能:${this.skill}`
    }
}

var stu = new Student("wuwei","逃课");
console.log(stu.name);
console.log(stu.showName())



拖拽例子:

function Drag(className) {

    this.dom = document.createElement('div');
    this.dom.className = className;
    document.body.appendChild(this.dom)

    this.disX = 0;
    this.disY = 0;

    // 初始化
    this.init()

}

Object.assign(Drag.prototype, {
    // 初始化函数
    init() {
        this.dom.onmousedown = function (e) {
            this.down(e)

            document.onmousemove = this.move.bind(this)
            document.onmouseup = this.up
        }.bind(this)
    },

    down(e) {
        // console.log(e.clientX, e.clientY)
        this.disX = e.clientX - this.dom.offsetLeft;
        this.disY = e.clientY - this.dom.offsetTop;
        // console.log(this.disX, this.disY)
    },

    move(e) {
        // console.log(this)
        this.dom.style.left = e.clientX - this.disX + 'px';
        this.dom.style.top = e.clientY - this.disY + 'px';
    },

    up(e) {
        // console.log(this)
        document.onmousemove = null;
        document.onmouseup = null;
    }
})


let left = new Drag('left')

ES 6

class Drag {
    constructor(className) {
        this.dom = document.createElement('div');
        this.dom.className = className;
        document.body.appendChild(this.dom)

        this.disX = 0;
        this.disY = 0;

        // 初始化
        this.init()
    }

    // 初始化函数
    init() {
        this.dom.onmousedown = function (e) {
            this.down(e)

            document.onmousemove = this.move.bind(this)
            document.onmouseup = this.up
        }.bind(this)
    }

    down(e) {
        // console.log(e.clientX, e.clientY)
        this.disX = e.clientX - this.dom.offsetLeft;
        this.disY = e.clientY - this.dom.offsetTop;
        // console.log(this.disX, this.disY)
    }

    move(e) {
        // console.log(this)
        this.dom.style.left = e.clientX - this.disX + 'px';
        this.dom.style.top = e.clientY - this.disY + 'px';
    }

    up(e) {
        // console.log(this)
        document.onmousemove = null;
        document.onmouseup = null;
    }
}

let left = new Drag('left')

通过继承创建有边界的拖拽

class LimitDrag extends Drag {
    move(e) {
        super.move(e);

        this.dom.style.left = Math.max(0, this.dom.offsetLeft) + 'px';
        this.dom.style.left = Math.min(window.innerWidth - this.dom.offsetWidth, this.dom.offsetLeft) + 'px';

        this.dom.style.top = Math.max(0, this.dom.offsetTop) + 'px';
        this.dom.style.top = Math.min(window.innerHeight - this.dom.offsetHeight, this.dom.offsetTop) + 'px';
    }
}

let right = new LimitDrag('right')
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,738评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,377评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,774评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,032评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,015评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,239评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,724评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,374评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,508评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,410评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,457评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,132评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,733评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,804评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,022评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,515评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,116评论 2 341