JavaScript(二十二)——JavaScript this 关键字

目录

方法中的 this

单独使用 this

函数中使用 this(默认)

函数中使用 this(严格模式)

事件中的 this

对象方法中绑定

显式函数绑定


面向对象语言中 this 表示当前对象的一个引用。

但在 JavaScript 中 this 不是固定不变的,它会随着执行环境的改变而改变。

在方法中,this 表示该方法所属的对象。

如果单独使用,this 表示全局对象。

在函数中,this 表示全局对象。

在函数中,在严格模式下,this 是未定义的(undefined)。

在事件中,this 表示接收事件的元素。

类似 call() 和 apply() 方法可以将 this 引用到任何对象。

方法中的 this

在对象方法中, this 指向调用它所在方法的对象。

示例:

var person = {
  firstName: "John",
  lastName : "Doe",
  id       : 5566,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};

this 指向了 person 对象。

因为 person 对象是 fullName 方法的所有者。

单独使用 this

单独使用 this,则它指向全局(Global)对象。

严格模式下,如果单独使用,this 也是指向全局(Global)对象

示例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>


</head>

<body>

    <p id="demo"></p>

</body>
<script>
    "use strict";
    var x = this;
    document.getElementById("demo").innerHTML = x;
</script>


</html>

运行结果:

函数中使用 this(默认)

在函数中,函数的所属者默认绑定到 this 上。

在浏览器中,window 就是该全局对象为 [object Window]

示例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>


</head>

<body>

    <p id="demo"></p>

</body>
<script>
    document.getElementById("demo").innerHTML = myFunction();
    function myFunction() {
        return this;
    }
</script>


</html>

运行结果:

函数中使用 this(严格模式)

严格模式下函数是没有绑定到 this 上,这时候 this 是 undefined

示例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>


</head>

<body>

    <p id="demo"></p>

</body>
<script>
    "use strict";
    document.getElementById("demo").innerHTML = myFunction();
    function myFunction() {
        return this;
    }
</script>


</html>

运行结果:

事件中的 this

在 HTML 事件句柄中,this 指向了接收事件的 HTML 元素

实例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>


</head>

<body>

    <button onclick="this.style.display='none'">点我后我就消失了</button>

</body>
</html>

运行结果:

对象方法中绑定

示例:

var person = {
  firstName  : "John",
  lastName   : "Doe",
  id         : 5566,
  myFunction : function() {
    return this;
  }
};

this 是 person 对象,person 对象是函数的所有者

示例:

var person = {
  firstName: "John",
  lastName : "Doe",
  id       : 5566,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};

this.firstName 表示 this (person) 对象的 firstName 属性

显式函数绑定

在 JavaScript 中函数也是对象,对象则有方法,apply 和 call 就是函数对象的方法。这两个方法异常强大,他们允许切换函数执行的上下文环境(context),即 this 绑定的对象。

bind也是,不过bind用于返回一个新函数,该函数调用时会将原函数的this值绑定为bind的第一个参数值。bind通常用于创建一个新的,永久绑定了特定this值的函数,比如事件处理函数

示例:

var person1 = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}
var person2 = {
  firstName:"John",
  lastName: "Doe",
}
person1.fullName.call(person2);  // 返回 "John Doe"

当我们使用 person2 作为参数来调用 person1.fullName 方法时, this 将指向 person2, 即便它是 person1 的方法

  • 23
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值