call_apply_bind

call 将a函数内部的this指向重新定位到第一个参数上,第二参数为传入函数参数的值

function a(name){
	console.log(this.y)
	console.log(name)
}
a.call({y:100},10)

apply 和call基本相同知识apply的第二个参数类型为数组类型

function a(name){
	console.log(this.y)
	console.log(name)
}
a.apply({y:100},[123])//100 123

bind 同样是将this的指向重新定位到bind的第一个参数对象,但是与call和apply不同的是他不会立即执行而是将改变this指向后的对象保存下来。

function a(name){
	console.log(this.y)
	console.log(name)
}
const c = a.bind({y:100})
c(123)//100  123
发布了7 篇原创文章 · 获赞 5 · 访问量 226

猜你喜欢

转载自blog.csdn.net/weixin_42362211/article/details/102780397
今日推荐