Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this 指向 #7

Open
LahmYing opened this issue Aug 9, 2019 · 0 comments
Open

this 指向 #7

LahmYing opened this issue Aug 9, 2019 · 0 comments
Labels
JS JavaScript

Comments

@LahmYing
Copy link
Owner

LahmYing commented Aug 9, 2019

ES5 中这两个执行函数其实是语法糖

记住!执行时!执行调用时才会牵扯到 this 指向!可看最后那个例子!

func(p1, p2)
obj.child.method(p1, p2)

其实它们最终是这样的

func.call(context, p1, p2)
// this 是 context,这里 context 为 undefined
// func.call(undefined, p1, p2)  
obj.child.method.call(obj.child, p1, p2)  
//  this 是 obj.child

对于 func.call(undefinded, p1, p2)

1565350344(1)
结果不是 undefinded 而是 window,这是因为规定:

call 方法的 context 如果是 null 或 undefined,那就将 context 默认为 window 对象。严格模式下,context 默认为 undefined

最后的例子

微信截图_20190810164722

参考

https://zhuanlan.zhihu.com/p/23804247

@LahmYing LahmYing added the JS JavaScript label Aug 9, 2019
@LahmYing LahmYing changed the title this 指向(未完) this 指向 Aug 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JS JavaScript
Projects
None yet
Development

No branches or pull requests

1 participant