In JavaScript, call, apply and bind are the three methods that come with the Function object. The main function of these three methods is to change the this pointer in the function, so as to achieve the effect of `taking over flowers and removing trees'. This article will explain these three methods in detail and list several classic application scenarios.
call(thisArgs [,args...])
This method can pass a thisArgs parameter and a parameter list. thisArgs specifies the caller of the function at runtime, that is, the this object in the function, and the parameter list will be passed into the calling function. The value of thisArgs has the following four situations:
(1) Do not pass, or pass null, undefined, this in the function points to the window object
(2) Pass the function name of another function, and this in the function points to a reference to this function
(3) Pass basic types such as strings, numbers, or Boolean types. This in the function points to its corresponding packaging object, such as String, Number, Boolean
(4) Pass an object, and this in the function points to this object
function a(){ console.log(this); //输出函数a中的this对象 } function b(){} //定义函数b var obj = {name:'onepixel'}; //定义对象obj a.call(); //window a.call(null); //window a.call(undefined);//window a.call(1); //Number a.call(''); //String a.call(true); //Boolean a.call(b);// function b(){} a.call(obj); //Object
This is the core function of call. It allows you to call an undefined method on an object, and this method can access the properties in the object. As for the benefits of doing this, I will talk about it later. Let’s look at a simple example first:
var a = { name:'onepixel', //定义a的属性 say:function(){ //定义a的方法 console.log("Hi,I'm function a!"); } }; function b(name){ console.log("Post params: "+ name); console.log("I'm "+ this.name); this.say(); } b.call(a,'test'); >> Post params: test I'm onepixel I'm function a!
When b.call is executed, the string `test` is passed to function b as a parameter. Due to the function of call, this in function b points to object a, so it is equivalent to calling function b on object a. In fact, b is not defined in a.
apply(thisArgs[,args[]])
The only difference between apply and call is the way the second parameter is passed. The second parameter of apply must be an array, while call allows a parameter list to be passed. It is worth your attention that although apply receives a parameter array, when it is passed to the calling function, it is passed in the form of a parameter list. Let’s look at a simple example:
function b(x,y,z){ console.log(x,y,z); } b.apply(null,[1,2,3]); // 1 2 3
This feature of apply is very important, we will mention this feature in the following application scenarios.
bind(thisArgs [,args...])
bind is a new method in ES5. Its parameters are similar to call, but it is significantly different from call/apply. That is, calling call or apply will automatically execute the corresponding function, while bind will not execute the corresponding function. The function just returns a reference to the function. At a glance, bind seems to lag behind call/apply, so why does ES5 introduce bind?
In fact, the real purpose of introducing bind in ES5 is to make up for the shortcomings of call/apply. Since call/apply will automatically execute the target function, it cannot be used in the event binding function because the event binding function does not need We execute it manually, it is executed automatically by JS internally when the event is triggered. Bind will not automatically execute the target function while changing the function this, so it can perfectly solve the above problems. You can understand it by looking at an example:
var obj = {name:'onepixel'}; /** * 给document添加click事件监听,并绑定onClick函数 * 通过bind方法设置onClick的this为obj,并传递参数p1,p2 */ document.addEventListener('click',onClick.bind(obj,'p1','p2'),false); //当点击网页时触发并执行 function onClick(a,b){ console.log( this.name, //onepixel a, //p1 b //p2 ) }
When the web page is clicked, onClick is triggered and executed, and onepixel p1 p2 is output, indicating that this in onClick has been changed by bind into an obj object. In order to have an in-depth understanding of bind, let’s take a look at the polyfill implementation of bind:
if (!Function.prototype.bind) { Function.prototype.bind = function (oThis) { var aArgs = Array.prototype.slice.call(arguments, 1), fToBind = this, //this在这里指向的是目标函数 fBound = function () { return fToBind.apply( //如果外部执行var obj = new fBound(),则将obj作为最终的this,放弃使用oThis this instanceof fToBind ? this //此时的this就是new出的obj : oThis || this, //如果传递的oThis无效,就将fBound的调用者作为this //将通过bind传递的参数和调用时传递的参数进行合并,并作为最终的参数传递 aArgs.concat(Array.prototype.slice.call(arguments))); }; //将目标函数的原型对象拷贝到新函数中,因为目标函数有可能被当作构造函数使用 fBound.prototype = this.prototype; //返回fBond的引用,由外部按需调用 return fBound; }; }
Application scenario one: Inheritance
As we all know, JavaScript does not have the extend keyword in high-level languages such as Java and C#, so there is no concept of inheritance in JS. If inheritance is necessary, call and apply can achieve this function:
function Animal(name,weight){ this.name = name; this.weight = weight; } function Cat(){ Animal.call(this,'cat','50'); //Animal.apply(this,['cat','50']); this.say = function(){ console.log("I am " + this.name+",my weight is " + this.weight); } } var cat = new Cat(); cat.say();//I am cat,my weight is 50
当通过new运算符产生了cat时,Cat中的this就指向了cat对象,而继承的关键是在于Cat中执行了Animal.call(this,'cat','50') 这句话,在call中将this作为thisArgs参数传递,于是Animal方法中的this就指向了Cat中的this,而cat中的this指向的是cat对象,所以Animal中的this指向的就是cat对象,在Animal中定义了name和weight属性,就相当于在cat中定义了这些属性,因此cat对象便拥有了Animal中定义的属性,从而达到了继承的目的。
应用场景二:移花接木
在讲下面的内容之前,我们首先来认识一下JavaScript中的一个非标准专业术语:ArrayLike(类数组/伪数组)
ArrayLike 对象即拥有数组的一部分行为,在DOM中早已表现出来,而jQuery的崛起让ArrayLike在JavaScript中大放异彩。ArrayLike对象的精妙在于它和JS原生的Array类似,但是它是自由构建的,它来自开发者对JavaScript对象的扩展,也就是说:对于它的原型(prototype)我们可以自由定义,而不会污染到JS原生的Array。
ArrayLike对象在JS中被广泛使用,比如DOM中的NodeList, 函数中的arguments都是类数组对象,这些对象像数组一样存储着每一个元素,但它没有操作数组的方法,而我们可以通过call将数组的某些方法`移接`到ArrayLike对象,从而达到操作其元素的目的。比如我们可以这样遍历函数中的arguments:
function test(){ //检测arguments是否为Array的实例 console.log( arguments instanceof Array, //false Array.isArray(arguments) //false ); //判断arguments是否有forEach方法 console.log(arguments.forEach); //undefined // 将数组中的forEach应用到arguments上 Array.prototype.forEach.call(arguments,function(item){ console.log(item); // 1 2 3 4 }); } test(1,2,3,4);
除此之外,对于apply而言,我们上面提到了它独有的一个特性,即apply接收的是数组,在传递给调用函数的时候是以参数列表传递的。 这个特性让apply看起来比call 略胜一筹,比如有这样一个场景:给定一个数组[1,3,4,7],然后求数组中的最大元素,而你知道,数组中并没有获取最大值的方法,一般情况下,你需要通过编写代码来实现。而我们知道,Math对象中有一个获取最大值的方法,即Math.max(), max方法需要传递一个参数列表,然后返回这些参数中的最大值。而apply不仅可以将Math对象的max方法应用到其他对象上,还可以将一个数组转化为参数列表传递给max,看代码就能一目了然:
var arr = [2,3,1,5,4]; Math.max.apply(null,arr); // 5
以上便是call和apply比较经典的几个应用场景,熟练掌握这些技巧,并把这些特性应用到你的实际项目中,会使你的代码看起来更加耐人寻味!