Valid JavaScript Item 21 uses the apply method to call a function to pass in the Variable Parameter List. Valid tiveapply

Source: Internet
Author: User

Valid JavaScript Item 21 uses the apply method to call a function to pass in the Variable Parameter List. Valid tiveapply

This series serves as the Reading Notes for objective JavaScript.

 

The following is a typical example of a method with a variable parameter list:


average(1, 2, 3); // 2average(1); // 1average(3, 1, 4, 1, 5, 9, 2, 6, 5); // 4average(2, 7, 1, 8, 2, 8, 1, 8); // 4.625

The following is an example of accepting only one array as a parameter:


averageOfArray([1, 2, 3]); // 2averageOfArray([1]); // 1averageOfArray([3, 1, 4, 1, 5, 9, 2, 6, 5]); // 4averageOfArray([2, 7, 1, 8, 2, 8, 1, 8]); // 4.625

Without a doubt, the method of having a variable parameter list is more concise and flexible, and it can process any number of parameters. But when a parameter is an array, how can I call a method with a variable parameter list?

 

The answer is to use the built-in apply method. This method is very similar to the call method, except that the apply method accepts an array as the parameter, then, each object in the array is called as a separate parameter. At the same time, the first parameter of the apply method has the same meaning as the first parameter of the call method. It is used to specify the point of this. Therefore, you can use the apply method to process parameters of the array type:


var scores = getAllScores();average.apply(null, scores);

If scores is an array with three elements, the above call is actually:


average(scores[0], scores[1], scores[2]);

Another example is to apply to the method that depends on the arguments variable. For the meaning and usage of the arguments variable, see Item 22.


var buffer = {state: [],append: function() {for (var i = 0, n = arguments.length; i < n; i++) {this.state.push(arguments[i]);}}};

The append method can be called using any number of parameters because it depends on the arguments variable in implementation:


buffer.append("Hello, ");buffer.append(firstName, " ", lastName, "!");buffer.append(newline);

You can call the apply method as follows:


buffer.append.apply(buffer, getInputStrings());

Note that when applying is called, the buffer object is passed as the point of this, because this variable is dependent on the append implementation, the dependency must be explicitly passed in to ensure that the modification occurs on the correct object.

 

Summary:

  1. Use the apply method to pass Parameters of the array type to the method that accepts the variable parameter list.
  2. Use the first parameter of the apply method to specify the point of this


For JavaScript method calls, it is best not to use global function calls.

What about the image? It is best to paste the code

C # Is there any optional parameter in the method, just like javascript? What should I do if I want to pass it in or out of the call?

Example 1: variable parameters:
Public void MethodA (string a, params int [] B );
Here, parameter a is required, and parameter B is variable (the variable parameter must be placed at the end of the parameter list, and only one variable parameter can be included in a function). The call can be as follows:
MethodA ("text ");
MethodA ("text", 1 );
MethodA ("text", 1, 2 );
MethodA ("text", 1, 2, 3 );

Example 2: default parameters:
Public void MethodB (string a = "default text ");
The call can be as follows:
MethodB ();
MethodB ("other text ");

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.