Colleagues got stuck because of the problem pointed by thisbug
,vue2’s this pointed problem
, using the arrow function, resulting in the inability to get the correspondingprops
. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. I'll share it with you todaythis
Point, if you haven’t learned anything, please give me a big mouth.
this
The pointer has nothing to do with where it is defined, it has to do with how it is called and in what formthis
(this) how this function is called (convenient to remember)We introduced above,this
Pointing mainly depends on the form of calling. Next, I will introduce the calling rules to you. Without rules, nothing is complete. Just keep these calling rules in mind. There is nothing difficult.
Functions are the most commonly used The calling method, the type of calling function: independent function call
function bar() { console.log(this) // window }
window
this
here isundefined
const info = { fullName: 'ice', getName: function() { console.log(this.fullName) } } info.getName() // 'ice'
The call is initiated and implicit binding is performed, so the current
thisis
info, and the value accessed through
this.fullNameis undoubtedly
ice
Implicit loss normal
In some cases, implicit loss will be performed, and the implicitly bound function will lose the bound object. , that is to say, it becomes the default binding. Thethisvalue of the default binding is
windowor
undefined, depending on the environment you are currently in. Whether it is strict mode.
const info = { fullName: 'ice', getName: function() { console.log(this.fullName) } } const fn = info.getName fn() //undefined
is found through
infoand assigned to the variable
fn
, from
windowTake out the
fullNameattribute, which must be
undefined
Implicit loss advancedWhat do you need to understand first? Is the callback function. In fact, it can be understood this way, that is, I don't call it now, but pass it to other places in the form of parameters, and call it somewhere else.
//申明变量关键字必须为var var fullName = 'panpan' const info = { fullName: 'ice', getName: function() { console.log(this.fullName) } } function bar(fn) { //fn = info.getName fn() // panpan } bar(info.getName)
in
baris a callback function
parameter passing It is an implicit assignment. In fact, it has the same meaning as the implicit loss above. They all point to
fn = info.getNamereferences, which is their memory addresses
is lost, that is, the function is called independently, the default binding rule is,
thisis the global
windowobject
?
will be added to the global
windowobject
is used, then No, I will introduce the two keywords for declaring variables in detail
thisare unexpected. In fact, we There is no control over how the callback function is executed, so there is no way to control that the calling location has the expected binding that this points to.
call/apply/bindare the methods on the function prototype chain, and they can be called in the function.
call()
方法使用一个指定的this
值和单独给出的一个或多个参数来调用一个函数。
this
对象apply()
方法类似,只有一个区别,就是call()
方法接受的是一个参数列表,而apply()
方法接受的是一个包含多个参数的数组。var fullName = 'panpan' const info = { fullName: 'ice', getName: function(age, height) { console.log(this.fullName, age, height) } } function bar(fn) { fn.call(info, 20, 1.88) //ice 20 1.88 } bar(info.getName)
call
的方法类似,只是参数列表有所不同
call
参数为单个传递apply
参数为数组传递var fullName = 'panpan' const info = { fullName: 'ice', getName: function(age, height) { console.log(this.fullName, age, height) } } function bar(fn) { fn.apply(info, [20, 1.88]) //ice 20 1.88 } bar(info.getName)
bind
与apply/call
之间有所不同,bind
传入this
,则是返回一个this
绑定后的函数,调用返回后的函数,就可以拿到期望的this。bind
时,可以传入参数bind
返回的参数也可以进行传参var fullName = 'panpan' const info = { fullName: 'ice', getName: function(age, height) { console.log(this.fullName, age, height) //ice 20 1.88 } } function bar(fn) { let newFn = fn.bind(info, 20) newFn(1.88) } bar(info.getName)
谈到new
关键字,就不得不谈构造函数,也就是JS中的 "类",后续原型篇章在跟大家继续探讨这个new关键字,首先要明白以下几点,new Fn()
的时候发生了什么,有利于我们理解this
的指向。
创建了一个空对象
将this指向所创建出来的对象
把这个对象的[[prototype]] 指向了构造函数的prototype属性
执行代码块代码
如果没有明确返回一个非空对象,那么返回的对象就是这个创建出来的对象
function Person(name, age) { this.name = name this.age = age } const p1 = new Person('ice', 20) console.log(p1) // {name:'ice', age:20}
new Person()
的时候,那个this所指向的其实就是p1
对象function bar() { console.log(this) //info } const info = { bar: bar } info.bar()
widonw或者undefined
,变相的可以认为隐式绑定 > 默认绑定var fullName = 'global ice' const info = { fullName: 'ice', getName: function() { console.log(this.fullName) } } info.getName.call(this) //global ice info.getName.apply(this) //global ice info.getName.bind(this)() //global ice
function bar() { console.log(this) //123 } const newFn = bar.bind(123) newFn.call(456)
首先我们来说一下,为什么是和bind
比较,而不能对call
和apply
比较,思考下面代码
const info = { height: 1.88 } function Person(name, age) { this.name = name this.age = age } const p1 = new Person.call('ice', 20) //报错: Uncaught TypeError: Person.call is not a constructor
new绑定和bind绑定比较
const info = { height: 1.88 } function Person(name, age) { this.name = name this.age = age } const hasBindPerson = Person.bind(info) const p1 = new hasBindPerson('ice', 20) console.log(info) //{height: 1.88}
bind
对Person
进行了一次劫持,硬绑定了this为info
对象new
返回的固定this的函数new关键字
>bind
>apply/call
>隐式绑定
>默认绑定
首先箭头函数是ES6
新增的语法
const foo = () => {}
var fullName = 'global ice' const info = { fullName: 'ice', getName: () => { console.log(this.fullName) } } info.getName() //global ice
ice
ES6
的新特性,箭头函数不绑定this
,它的this
是上一层作用域,上一层作用域为window
global ice
getObjName
通过this
拿到info
中的fullName
(值为ice
的fullName
)const info = { fullName: 'ice', getName: function() { let _this = this return { fullName: 'panpan', getObjName: function() { console.log(this) // obj console.log(_this.fullName) } } } } const obj = info.getName() obj.getObjName()
当我调用info.getName()
返回了一个新对象
当我调用返回对象的getObjName
方法时,我想拿到最外层的fullName
,我通过,getObjName
的this访问,拿到的this却是obj
,不是我想要的结果
我需要在调用info.getName()
把this保存下来,info.getName()
是通过隐式调用,所以它内部的this就是info对象
getObjName
是obj对象,因为也是隐式绑定,this必定是obj对象,绕了一大圈我只是想拿到上层作用域的this而已,恰好箭头函数解决了这一问题
const info = { fullName: 'ice', getName: function() { return { fullName: 'panpan', getObjName: () => { console.log(this.fullName) } } } } const obj = info.getName() obj.getObjName()
默认绑定
隐式绑定
显示绑定 apply/call/bind(也称硬绑定)
new绑定
new绑定
bind
call/apply
隐式绑定
默认绑定
当一切都看起来不起作用的时候,我就会像个石匠一样去敲打石头,可能敲100次,石头没有任何反应,但是101次,石头可能就会裂为两半 我知道并不是第101次起了作用,而是前面积累所致。
大家有疑惑可以在评论区留言 第一时间为大家解答。
(学习视频分享:web前端开发)