javascript之apply和call

以前看this指向的时候看过apply和call。

apply func.apply(thisArg, [argsArray])

将func中的this指向thisArg,arguments为[argsArray]。

call function.call(thisArg, arg1, arg2, ...)

将func中的this指向thisArg,arguments为arg1arg2, ...。

不求甚解的我本来就这么过去了,直到。。。看到这句话:

thisArgOptional. The value of this provided for the call to a function. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode , null and undefined will be replaced with the global object and primitive values will be converted to objects.

简言之,在JavaScript的非严格模式下,这个thisArg如果设置成了null,undefined,会被封装成全局对象呈给this;在严格模式下,这个thisArg设置成了什么,那就是什么了。

mk,以防之后踩坑。

火狐开发者文档  里边的解释简单&易懂&全面,还能顺带练习英语,实乃攻城掠地必备良品。

原文地址:https://www.cnblogs.com/Merrys/p/8472082.html