this指向及改变this指向的三个方法call、apply、bind

文章详细介绍了JavaScript中this的常见指向,包括全局、事件、函数、箭头函数和构造函数中的行为,并讨论了如何通过call、apply和bind来改变this的指向。通过对不同场景下的示例分析,阐述了JavaScript中上下文可变的特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、this指向

1、全局 --->window

console.log(this);   //Window对象

2、事件 --->事件源

<body>
    <button>this指向</button>
</body>
<script>
    var btn = document.querySelector('button');
    btn.onclick = function() {
        console.log(this);    // <button>this指向</button>
    }
</script>

3、函数 --->谁调用指向谁,没有默认window,函数内严格模式为undefined

let People = {
    name : '张三',
    age : 18,
    sex (){
        console.log(this);   // {name: '张三', age: 18, sex: ƒ}
        setTimeout(function () {
			console.log(this)    // window对象
		})
        setTimeout(()=> {
			console.log(this)   // {name: '张三', age: 18, sex: ƒ}
		})
    }
}
People.sex()


function testThis(){
    console.log(this);    // window对象
}
testThis()


function testThis(){
    'use strict'
    console.log(this);    // undefined 
}
testThis()

4、箭头函数 --->定义时所处函数环境

let btn = document.querySelector('button');
btn.onclick = ()=>{
    console.log(this);   // window对象
}


let People = function(name) {
    this.name = name;
    this.age = 18;
    this.sex = () => {
        console.log(this);   // People {name: '张三', age: 18, sex: ƒ} 
    }
}
let People1 = new People('张三');
People1.sex()

5、构造函数 --->实例对象

let People = function(name) {
    this.name = name;
    this.age = 18;
    this.sex = function(){
        console.log(this);   // People {name: '张三', age: 18, sex: ƒ} 
    }
    console.log(this);   // People {name: '张三', age: 18, sex: ƒ} 
}
let People1 = new People('张三');
People1.sex()

二、改变 this 指向的三个方法

JavaScript 的一大特点是,函数存在「定义时上下文」和「运行时上下文」以及「上下文是可以改变的」这样的概念。

1、call

要改变this指向的函数.call(想要this指向的对象, 参数1, 参数2, 参数3…)

function People(name, age, sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
    console.log(this);    // a处
}
let People1 = {
    name: '张三',
    age: 18,
    sex: '未知',
    fun: function(){
        console.log(this);    // b处
    }
}
People1.fun()    // b处 {name: '张三', age: 18, sex: '未知', fun: ƒ}
People.call(People1, '李四',14,'男')    // a处 {name: '李四', age: 14, sex: '男', fun: ƒ}
People1.fun()    // b处 {name: '李四', age: 14, sex: '男', fun: ƒ}

2、apply

要改变this指向的函数.apply(想要this指向的对象, [参数1, 参数2, 参数3…])

function People(name, age, sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
    console.log(this);    // a处
}
let People1 = {
    name: '张三',
    age: 18,
    sex: '未知',
    fun: function(){
        console.log(this);    // b处
    }
}
People1.fun()    // b处 {name: '张三', age: 18, sex: '未知', fun: ƒ}
People.apply(People1,['李四',14,'男'])    // a处 {name: '李四', age: 14, sex: '男', fun: ƒ}
People1.fun()    // b处 {name: '李四', age: 14, sex: '男', fun: ƒ}

 3、bind

要改变this指向的函数.bind(想要this指向的对象, 参数1, 参数2, 参数3…)

不会调用函数,返回是函数

function People(name, age, sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
    console.log(this);    // a处
}
let People1 = {
    name: '张三',
    age: 18,
    sex: '未知',
    fun: function(){
        console.log(this);    // b处
    }
}
People1.fun()    // b处 {name: '张三', age: 18, sex: '未知', fun: ƒ}
let People2 = People.bind(People1, '李四',14,'男')    // 无
People2()    // a处 {name: '李四', age: 14, sex: '男', fun: ƒ}
People1.fun()    // b处 {name: '李四', age: 14, sex: '男', fun: ƒ}

 4、三者区别

方法参数是否执行函数返回值
call想要this指向的对象, 参数1, 参数2, 参数3…原函数定义的返回值
apply想要this指向的对象, [参数1, 参数2, 参数3…]原函数定义的返回值
bind想要this指向的对象, 参数1, 参数2, 参数3…新的函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃彩虹吐司的安琪拉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值