The call and apply in JS

Source: Internet
Author: User

Read the description of the call and apply method in the JS authoritative guide: we can call () and apply () as a method of an object, calling the function indirectly in the form of a calling method. This explanation is a bit confusing. Here's your opinion: in fact, call and apply are literally meant to represent the callback of a function, in fact, the use and AS3 of the same call and apply. Where the first parameter of call () is the same as the first parameter of apply (), which represents the parent object of the function to be called, he is the context of the call, and in the body of the function obtains a reference to him by using this. The rest of the call () parameter is the argument list to be passed to the called function. Apply () A parameter other than the first argument is passed to the argument of the calling function, except that he is in the form of an array, and the parameter list of call () is different.

To invoke the function fu () as an object Ob method, you can use call and apply.

Fu.call (OB); fu.apply (OB);

In strict mode, the first parameter of call and apply will change to the value of this. Even if the actual argument passed in is the original value or null,undefined. Null and undefined passed in non-strict mode are replaced by global objects, and the original values are replaced by the corresponding wrapper objects.

Example one:

 var  func=new  function  () {this . A= "func"  Span style= "color: #0000ff;"  >var  myfunc=function   (x) { var  a= "MyFunc" ;             var  b = "bbbbb" ;            Alert ( this  .b);            Alert ( this  .a);        alert (x);  } myfunc.call (func,  "var"); *//*  var        Nu = Math.max.apply (math,[1,3,4,6,7]); Alert (NU);  

Example two:

var nu = Math.max.apply (math,[1,3,4,6,7]); alert (NU);

Example three:

function Add (A, b)        {            + b);        }         function Sub (A, b)        {            - b);        }         3, 1);

Run to see what the results are, and you'll understand.

Http://www.cnblogs.com/huyong/p/4139875.html

The call and apply in JS

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.