理解this

什么时候用this?
//构造函数 new 原型
会发现new会改变this指向,并且优先级最高

//bind call apply
这三也可以改变this指向

//对象
谁调用指向谁

//函数
this的指向window 因为在执行期上下文中,
由于没人调用,所有会有一个默认绑定
this ==》window

总结:
默认绑定: this ==》window
隐试绑定: 谁调用指向谁
显示绑定: bind apply call
new 改变this指向!!!

var module = {
x: 42,
getX: function() {
return this.x;
}
}

var unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// expected output: undefined

var boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// expected output: 42

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值