Javascript apply, call, and arguments objects

Source: Internet
Author: User

1. "methods" are just functions that are assigned to a property of an object.
Example:

VaR slideshow = function () {init: function () {This. start () ;}, start: function () {},}; window. onload = slideshow. init; // call this. in start (), this is the window object.

 

2. The call and apply functions are basically the same, but the difference is that the parameter passing method is as follows (FOO is a function ):

foo.call(obj, arg1, arg2) == foo.apply(obj, arguments)

That is, the second parameter apply is a parameter array. OBJ is the object that calls the foo function.

According to the first point, when the method of an object is executed in Javascript, The this pointer is not necessarily the object, but must be determined based on which object is calling the current method.

Call and apply take advantage of this.

 

3. arguments-another flexible way to obtain function parameters

A simple demo is written as follows:

Function add (a, B) {for (VAR V in arguments) {// v is a parameter index, starting from 0. log (arguments [v]) ;}} add (3, 4); // output 3 4add (3, 4, 5); // output 3 4 5 function Add2 () {for (var v in arguments) {// V is the parameter index, starting from 0. log (arguments [v]) ;}} Add2 (3, 4); // output 3 4add2 (3, 4, 5); // output 3, 4 5

The above shows that there are no strict restrictions on JavaScript function parameters. For function add (a, B), A and B only identify the function with two parameters, and the code is readable by correctly naming a and B.

Even so, we can still pass parameters without following its requirements.

In general, the use of arguments greatly enhances the flexibility of passing parameters to the function.

 

4. Specific Application of arguments:

Many API functions of jquery provide optional parameters (provided through objects such as {age: '22', sex: 'male'}). This implementation is achieved through arguments, below is another simple demo (from slidemo-.net

 

Function validate () {var options ={}; var fields = arguments; // steal slice from array. prototype var slice = array. prototype. slice; If (typeof fields [fields. length-1] = 'object') {Options = Fields [fields. length-1]; fields = slice. call (fields, 0, fields. length-1);} console. log ("fields:"); For (VAR fkey in fields) {console. log (fields [fkey]);} console. log ("Options:"); For (VAR okey in options) {console. log (options [okey]) ;}} validate ('name', 'email '); // No configuration item validate ('name', 'email', {min: 4, MAX: 10}); // There are configuration items

 

Of course, if you want to do better, you can provide a default configuration item object, so that when the caller does not provide a configuration item object, use the default configuration item.

However, note that you can access the parameter list through arguments only through the index, but not through the parameter identifier (that is, the parameter name ).

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.