🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# arguments 函数在运行时,会在函数体内定义一个arguments变量,其内容是调用该函数时传入的参数。 ~~~ var func = function () { console.dir(arguments); }; func();//[] func(10);//[10] func(10, "ES", true);//[10,"ES",true] ~~~ # call和apply ES中的函数是以对象的形式存在的。 ~~~ var func = function () { }; console.dir(func); ~~~ ![](https://box.kancloud.cn/03461b68aa03dafddc3d532207fc1180_208x128.jpg) Function类对象有两个常用的成员函数,call和apply,用于“显式”调用函数。 他们行为是一样的,但是在使用上存在着差别。 ~~~ var func = function () { console.dir(this); console.dir(arguments); }; var foo = { foo: 'I am foo' }; //call(thisRef,...[argus]); func.call(foo, 1, 2, 3); console.log('---------'); //apply(thisRef,[argus]) func.apply(foo, [1, 2, 3]); ~~~ ![](https://box.kancloud.cn/04c379f9ab95eb2f5d8f94f3f7a45568_213x162.jpg) 与一般情况下使用函数时的“隐式”调用结果一样。 ~~~ foo.func = func; foo.func(1, 2, 3); ~~~ ![](https://box.kancloud.cn/122f843f4d4f724d5dedb89c09e00491_419x199.jpg) 因此,把函数的调用看作是call或apply函数的使用,this几乎和一般的参数一样。 这样一来,this的绑定行为就不会显得很特别。 # this与装箱 ~~~ var func = function () { console.dir(this); }; func.call(2017);//Number类对象 Number.prototype.func = func; var num = 2017; num.func();//Number类对象 ~~~ 原始值作为this的绑定传入函数前,会进行一次装箱,将新构造的对象与this关键字进行绑定。 一如前文所述,this关键字的作用是获取调用函数的对象。