this和箭头函数的this

2023-03-08,

https://www.cnblogs.com/lfri/p/11872696.html

https://www.ruanyifeng.com/blog/2018/06/javascript-this.html

函数调用了,才会产生this。这个this,就是指向的是这个谁在调用这个函数。这个函数在这个谁的上下文环境下被调用了。然后在这个函数作用域内,可以通过this去访问这个谁。

这个this,虽然指向的是外部的上下文环境,但是这个this只存在在函数体内。

const obj = {
a: function () {
console.log(1111, this)
// 因为a函数被调用了,是在obj这个context调用,所以this就是obj // function b () {
// console.log(1111, this)
// }
b = () => {
console.log(2222, this)
}
b()
// b.call() 这里的this肯定从外层传进来的。b()外层的this就是obj。 function c () {
console.log(3333, this)
}
c()
// c.call() 这里的参数没有传值,就是undefined,默认就会是window作为context,所以this就是window }
} obj.a()
// obj.a.call(obj)

this和箭头函数的this的相关教程结束。

《this和箭头函数的this.doc》

下载本文的Word格式文档,以方便收藏与打印。