< turn > difference between apply and call

Source: Internet
Author: User

Original address: The difference between https://github.com/lin-xin/blog/issues/7apply and call

The ECMASCRIPT specification defines call and apply two methods for all functions, their application is very extensive, their role is exactly the same, but the form of communication is different.

Apply ()

The Apply method passes two parameters: one is an object as a function context, and the other is an array of function arguments.

var obj = {    ' linxin '}function  func (firstName, lastName) {      this. Name + ' + lastName ');} Func.apply (obj, [' A ', ' B ']);    // A linxin B

As you can see, obj is the object that is the function context, and this is the object in function func that points to obj. Parameters A and B are placed in the array to pass in the Func function, corresponding to the list element of the Func parameter, respectively.

Call ()

The first parameter of the call method is also the object that is the function context, but the subsequent pass is a list of parameters, not a single array.

var obj = {    ' linxin '}function  func (firstName, lastName) {      this. Name + ' + '+ ' C ', ' D ');       // C linxin D

Compare apply we can see the difference, C and D are passed as separate arguments to the Func function instead of being placed in the array.

For when to use what method, in fact, do not tangle. If your parameters are already in an array, then use apply, and if the parameters are scattered, there is nothing to associate with call.

Usage of apply and call 1. Change this point
var obj = {    ' linxin '}function  func () {    Console.log (this  . name);} Func.call (obj);        // linxin

We know that the first parameter of the call method is the object that is the function context, where obj is passed to Func as an argument, and this in the function points to the Obj object. The Func function here is actually equivalent to

function func () {    console.log (obj.name);}

2. Ways to borrow other objects

First look at the example

var Person1  function  () {    this. Name = ' Linxin ';}  var function () {    thisfunction  () {        Console.log (this. Name) ;    }    Person1.call (this);} var New Person2 ();p erson.getname ();        // linxin

From the above we can see that the object Person2 instantiated by the GetName method gets the name in the Person1. Because the role of Person1.call (this) in Person2 is to use the Person1 object instead of the This object, Person2 has all the properties and methods in Person1, which is equivalent to Person2 inheriting the genus Person1 Sex and methods.

3. Calling functions

The Apply and call methods will make the functions execute immediately, so they can also be used to invoke functions.

function func () {    console.log (' linxin ');} Func.call ();             // linxin

The difference between call and bind

The method called bind is extended in ECMASCRIPT5 and is incompatible in the low version of IE. It is similar to call, which takes two parts, the first parameter is the object as the function context, the second part is a list and can accept multiple parameters.
The difference between them has the following two points.

1.bind return value is a function
var obj = {    ' linxin '}function  func () {    Console.log (this  . Name);} var func1 = func.bind (obj); func1 ();   
var obj = {    ' linxin '}function  func () {    Console.log (  this. name);} var func1 = func.bind (obj); func1 ();   

Linxin

The bind method does not execute immediately, but instead returns a function that changes the context of this. The This in the original function func is not changed and still points to the Global object window.

2. Use of parameters
function func (A, B, c) {    Console.log (A, B, c);} var func1 = Func.bind (null, ' linxin '); func (' A ', ' B ', ' C ');            // a B Cfunc1 (' A ', ' B ', ' C ');           // linxin A bfunc1 (' B ', ' C ');                // linxin B Cfunc.call (null, ' linxin ');      // linxin undefined undefined

Call is to pass the second and subsequent arguments as arguments to the Func method, and the actual argument of the Func1 method is in the back row based on the parameters of bind.

In the low-version browser does not have the bind method, we can also implement one ourselves.

if(!Function.prototype.bind) {Function.prototype.bind=function () {            varSelf = This,//Save original functioncontext = [].shift.call (arguments),//Save the This context that needs to be boundargs = [].slice.call (arguments);//the remaining arguments are converted to arrays            return function() {//returns a new functionself.apply (Context,[].concat.call (args, [].slice.call (arguments))); }        }    }

< turn > difference between apply and call

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.