小白学前端(二) 为什么对象方法里的函数this指向window
2018-05-08 本文已影响0人
OKOK熊
1. 函数里的函数中的 this 就是在 global 环境下的 window

2. 对象方法里的函数中的 this 为什么指向 window?

如果按照我的理解,那么infunc() 其实和 o 并没有关系
infunc() 在object里 其实就和在 window / global 中执行是等价的... 具体是为什么估计要等更加了解js的底层原理了...
还可以参考这篇文章 https://zhuanlan.zhihu.com/p/23804247
再赋一个例子



3. 对象方法里的箭头函数 this 却是动态绑定的?

有没有大神可以详细的解释下这是为什么?
另一个奇怪的this现象:

可以这么解释,如果只是o.f,那么x()就相当于是一个 全局函数
function x (){ return this.prop } x(); x调用发生在window中,window.prop不存在,即undefined
如果是o.f() 那么this.prop 就已经执行为 o.prop 了