Easy Learning javascript--Part 2nd: Rest parameters in a function

Source: Internet
Author: User
Tags unpack javascript array

JavaScript functions can use any number of parameters. Unlike other languages, such as C # and Java, you can pass any number of arguments when you call a JavaScript function. The JavaScript function allows an unknown number of function arguments. Before ECMAScript 6, JavaScript had a variable to access the unknown or variable number of arguments, an array-like object, not an array. Consider the following code to understand the arguments variable:

function add ( var result = 0; for (let i=0;i< arguments.length;i++) {result = result + arguments[i];} return result; var r = Add (6,9, 3,2); console.log (R); var t = Add (7,56,9); console.log (t);            

As you can see, the arguments object is used to access unknown or mutable function parameters. Even if arguments uses the length property and square brackets, it is not a true JavaScript array. You cannot use other JavaScript array methods for arguments objects, such as Pop,push,slice. Some of the problems that exist when using arguments are:

    • The JavaScript function arguments object is not a true JavaScript array, so you cannot use other array methods, such as Pop,push,slice.
    • It is difficult to access the arguments object of an external function in an intrinsic function. To access it, you need to assign the arguments function of the external function to the variable, and then use it in the intrinsic function.
    • If you want to use the arguments object as an array, you need to convert manually by Aarry.prototype.slice.

ECMAScript 6 introduces a new function, the rest parameter, which represents an unknown number of parameters as an array in the function. It not only represents the additional parameters as an array, but also resolves many of the problems with the arguments object. Use the rest parameter to override the Add function above.

function add (...theargs) {var result = 0; for (let i=0;i<theArgs.length ; i++) {result = result + Theargs[i];} return result; var r = Add (6,9, 3,2); console.log (R); var t = Add (7,56,9); console.log (t);            

You can define the rest parameter as ... Theargs or ... args. If the last named function parameter is in ... (three points) as the prefix, then it becomes the rest parameter of the function. The rest parameter of the JavaScript function is a pure JavaScript array. In the above code, ... Theargs is the rest parameter of the function add, because it is the only named parameter, and also in ... (Three dots) as a prefix. Since the rest parameter is a JavaScript array, you can perform operations such as Push,pop on the rest parameter Theargs, as shown in the following code:

function add(...theArgs){    theArgs.push(10); var result = 0; for(let i=0;i<theArgs.length;i++){ result = result + theArgs[i]; } var lastItem = theArgs.pop(); console.log(lastItem); return result; }

The rest parameter of the JavaScript function can also work with other parameters. If you do not want to include specific parameters in the rest parameter array, then you may need to use other named parameters in the function. Think of the following code block:

function add(num1, num2, ...theArgs){    console.log(num1); console.log(num2); console.log(theArgs.length);}var r = add(6,9,3,2);var t = add(7,56,9);

For the first function call, 6 and 9 are assigned to NUM1 and num2, respectively. For a second function call, 7 and 56 are assigned to NUM1 and num2. The parameter that starts the third parameter is assigned to the rest parameter array. Keep in mind that the first two parameters do not become part of the rest parameter array. So, if you're going to include all the values in the rest parameter, you should start by defining them as comma-delimited named arguments. The code given below will result in an error:

function add(num1, ...theArgs,num2){    console.log(num1); console.log(num2); console.log(theArgs.length);}

In the above code, the rest parameter is not the last parameter, so JavaScript throws an error. The rest parameter must be the last formal parameter.

Image

JavaScript allows you to break the rest parameters, which means that you can unpack the rest variable data into different variable names. Take a look at the following code:

function add(...[a,b,c]){    return a+b+c; }var r = add(6);console.log(r);var t = add(7,56,9);console.log(t);

The first time the function is called, a = 6,b = Undefined,c = undefined, the second function call, will be assigned a = 7,b = 56,c = 9. In this example, the function ignores any additional arguments that are passed.

The rest parameter of the JavaScript function is a huge improvement to the use of unknown parameters to the arguments object. It is a pure JavaScript array, so you can use all the array methods on it. You can unpack the rest variable data into a named variable. You can assign any name to the rest parameter, which is another significant improvement using the arguments keyword.

Welcome to join the Learning Exchange Group 569772982, we learn to communicate together.

Easy Learning javascript--Part 2nd: Rest parameters in a function

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.