没有合适的资源?快使用搜索试试~ 我知道了~
this this关键字,就是所谓的执行上下文。this关键字在函数中,表示的是一个指向,this的指向永远是一个对象。 哪个对象调用函数,函数里面的this指向哪个对象。 代码示例: function f() { let text=哈哈哈 console.log(this.text) // undefined console.log(this) // Window } f() 上面的代码等价于: function f() { let text=哈哈哈 console.log(this.text) // undefined console.log(this)
资源详情
资源评论
资源推荐
JS中的中的this指向问题指向问题 乛乛◡乛乛 老底都被掀出来了老底都被掀出来了
this
this关键字,就是所谓的执行上下文。this关键字在函数中,表示的是一个指向,this的指向永远是一个对象。
哪个对象调用函数,函数里面的this指向哪个对象。
代码示例:
function f() {
let text="哈哈哈"
console.log(this.text) // undefined
console.log(this) // Window
}
f()
上面的代码等价于:
function f() {
let text="哈哈哈"
console.log(this.text) // undefined
console.log(this) // Window
}
window.f()
f()函数实际上是被window对象调用的
var o = {
text:'呵呵',
f:function(){
console.log(this.text) // 呵呵
console.log(this) // {text: "呵呵", f: ƒ}
}
}
o.f()
这里this指向对象o,因为是对象o调用f()
上面代码等价于:
var o = {
text:'呵呵',
f:function(){
console.log(this.text) // 呵呵
console.log(this) // {text: "呵呵", f: ƒ}
}
}
window.o.f()
由此可以看出this指向的是最后调用它的对象
下面再来看看这个:
var o = {
text:'呵呵',
a:{
text:'嘿嘿',
f:function(){
console.log(this.text) // undefined
console.log(this) // Window
}
}
}
var fun = o.a.f
fun()
上面this指向window, 最下面的fun()等价于window.fun()。而this指向的是最后调用它的对象
function f(){
this.text="呵呵"
}
var o = new f()
console.log(o.text) // 呵呵
console.log(o) // f {text: "呵呵"}
上面,new关键字创建了一个对象实例o。new关键字会改变this的指向,这个时候指向对象o
假如,函数里有假如,函数里有return,,this指向会怎样?指向会怎样?
weixin_38687904
- 粉丝: 8
- 资源: 921
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0