js function 里面的this是什么

前端之家收集整理的这篇文章主要介绍了js function 里面的this是什么前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

js function里面的this是什么,主要看这个function是怎么调用

some = {}
some.method = method
some.method() // 输出some
method() // 输出global
method.call(some) // 输出some
some.method.apply({a:1}) // 输出 {a:1}

如果把 x.method() 这种叫做有主调用(主就是 . 前面的那个变量),那么函数里面的this就是
method()这些叫做无主调用,这时候this就是global
call apply这些方法function改变为第一个参数。fun.apply( obj )
strict mode,有this函数,是不能无主调用的,会出错
strict mode,带this函数无主调用this的值为undefined

es6里面的箭头函数里面的this,始终是前文的this,比如
[1,2,3].map(_=>this) // [global,global,global]

所以,你上面问的问题,无法判断this是啥,你没有给出fire是怎么调用

猜你在找的程序笔记相关文章