JS apply () and call ()

Source: Internet
Author: User

Properties and methods of functions

The function in JS is an object, so the function also has properties and methods.

Each function consists of two properties: length and prototype.

The Length property , which represents the number of named arguments that the function expects to receive.

The prototype property is the real place where all of their instance methods are saved.

In ECMAScript5, the prototype property is not enumerable.

Each function has two non-inherited methods: Apply () and call ().

The purpose of both methods is to invoke the function in a specific scope, and actually to set the value of the This object in the function body.

The apply () method receives two parameters: one is the scope in which the function is run, and the other is a parameter array (which can be an array instance or a arguments object).

The call () method receives two parameters: one is the scope in which the function is run, and the other is the argument that is passed to the function (which must be enumerated individually).

The Apply () method differs from the call () method only in the way that the parameter is received.

The real strength of apply () and call () is the ability to extend the scope in which functions run.

1Window.color = "Red";2 varn \ {3Color: "Green"4 };5 functionSaycolor () {6Console.log ( This. color);7 };8Saycolor ();//"Red"9Saycolor.call ( This);//"Red"TenSaycolor.call (window);//"Red" OneSaycolor.call (o);//"Green"

The biggest benefit of using apply () and call () to extend the scope is that the object does not need to have any coupling with the method.

Two methods allow you to explicitly specify the this value that is required for the invocation, that is, any function can be invoked as a method of any object, even if the function is not a method of that object at all.

A method is also defined in ECMASCRIPT5:bind (); This method creates an instance of a function whose this value is bound to the value passed to the bind () function.

For example:

 1  window.color = "Red"  2  var  o = 3  color: "Green" 4  }; 5  function   Saycolor () { this  .color);  7  };  8  var  x =    9  x (); //  

JS 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.