ES6this指向丢失问题+利用class类创建对象

1、在dom操作中使用箭头函数可以规避this指向丢失问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .box{
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <div id="odiv" class="box"></div>
    <script>
        var obj = document.getElementById("odiv");
        obj.onclick = function(){
            
            // setTimeout(function(){
            //     this.style.width="300px"   //修改不了
            // },1000);
​
​
            setTimeout(()=>{
                this.style.width="300px"   //修改成功
            },1000);
        }
    </script>
</body>
</html>
setTimeout(()=>{
            this.style.width="300px"   //修改成功
        },1000);
    }

箭头函数没有自己的作用域,即箭头函数 this 指向其外层作用域

因此this可以指向到事件源

2、es6面向对象(重点)

一般面向对象的语言里面,都有类,实例对象这些概念。我们通过实例化类,得到实例对象。

类:定义一个 类,需要使用 class 关键字,但是es5里面,使用构造函数(首字母大写)来实现的。

继承:一般使用 extends 关键字来进行继承,但是在es5里面,也是没有extends继承,而是使用的prototype原型继承完成的。

ES6之前的写法:

// 用函数定义类
function Animal(name){
    this.name = name;
}
// 给类的实例定义方法(利用原型链方法)
Animal.prototype.showName = function(){
    console.log(this.name);
}
// 给类定义静态方法(在es6中静态属性只能在class类外调用,且调用时语法格式为类名+属性名)
Animal.eat = function(){
    console.log('进食');
}
​
// 根据函数实例化对象
var a = new Animal("Tom");
// 调用对象的方法
a.showName();
// 调用类的静态方法
Animal.eat();

ES6的写法(掌握)

class Animal{
​
    // 定义构造函数,接收实例对象中的实参
    constructor(name){
        // console.log("constructor");
        this.name = name
    }
​
    // 定义一个方法
    showName(){
        console.log(this.name);
    }
​
    //定义静态方法
    static eat(){
        console.log("吃---");
    }
}
let obj = new Animal("Tom");
obj.showName();
Animal.eat();

14、关于继承

ES6之前的写法(仅做了解),利用extends 方法

function Animal(name){
    this.name = name;
}
Animal.prototype.showName = function(){
    console.log(this.name);
}
Animal.eat = function(){
    console.log('进食');
}
​
// 定义子类
function Mouse(name, color){ 
    // 子类继承父类的属性 需要将 this 指向父类中的 name
    Animal.call(this, name);  
    this.color = color;
}
// 子类继承父类的方法
Mouse.prototype = new Animal(); 
// 给子类实例添加新方法 
Mouse.prototype.showInfo = function(){
    console.log(this.name, this.color);
}
​
var m = new Mouse('Jerry', 'gray');
m.showName();
m.showInfo();
Animal.eat();

JS中es6之前使用原型链概念与此相同

CSDN

es6之后使用类的方法去实例化对象

ES6的写法:(掌握)

class Animal{
​
    // 定义构造函数
    constructor(name){
        // console.log("constructor");
        this.name = name;
    }
    // 定义一个方法
    showName(){
        console.log(this.name);
    }
​
    //定义静态方法
    static eat(){
        console.log("吃---");
    }
}
​
class Cat extends Animal{
​
}
​
let cat1 = new Cat("Tom");
​
cat1.showName();
Cat.eat();

class Cat extends Animal

解释:定义一个类为cat的函数,其内部方法和属性使用原先定义的Animal函数中的方法和属性,避免内存的浪费。使用远离和对同一个类多次实例对象调用其相同属性方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值