Our study or work will involve JavaScript, so we must also understand JavaScript. When it comes to rest parameters, they are a large part of the function. And functions are ever-changing. This article will help you understand and master JavaScript. Let's study it carefully.
JavaScript Functions can take any number of parameters. Unlike other languages such as C# and Java, you can pass any number of arguments when calling a JavaScript function. JavaScript functions allow an unknown number of function parameters. Prior to ECMAScript 6, JavaScript had a variable to access these unknown or variable numbers of parameters, which was 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 variable function parameters. Even though arguments uses the length attribute and square brackets, it is not a true JavaScript array. You cannot use other JavaScript array methods like pop, push, slice, etc. on the arguments object. Some problems when using arguments are:
The JavaScript function arguments object is not a real JavaScript array; therefore, you cannot use other array methods such as pop, push, slice, etc.
It is difficult to access the arguments object of an external function in internal functions. To access it, you need to assign the arguments function of the outer function in a variable and then use it in the inner function.
If you want to use the arguments object as an array, then you need to manually convert it via Aarry.prototype.slice.
ECMAScript 6 introduces a new feature, Rest parameters, which represent an unknown number of parameters as an array in a function. Not only does it represent additional parameters as arrays, it also solves many problems with arguments objects. Rewrite the add function above using the rest parameter.
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 rest parameters as...theArgs or...args. If the last named function parameter is prefixed with ... (three dots), then it becomes the rest parameter of the function. The rest parameter of a 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 is also prefixed with ... (three dots). Since the rest parameter is a JavaScript array, you can perform operations such as push, pop, etc. 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 don't want to include specific parameters in the rest parameter array, then you may need to use other named parameters in the function. Consider the following block of code:
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 will be assigned to num1 and num2 respectively. For the second function call, 7 and 56 will be assigned to num1 and num2. The parameter that starts the third parameter will be assigned to the rest parameter array. Keep in mind that the first two parameters will not be part of the rest parameter array. So, if you plan to include all values in rest parameters, you should define them as comma-separated named parameters from the beginning. 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 will throw an error. The rest parameter must be the last formal parameter.
JavaScript allows you to destroy rest parameters, which means you can unpack rest variable data into different variable names names. Please 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 function call will assign a = 6, b = undefined, c = undefined, and the second function call will assign a = 7, b = 56, c =9. In this example, the function will ignore any additional arguments passed.
The rest parameter of JavaScript functions is a huge improvement over the arguments object Using functions Unknown parameters. It is a pure JavaScript array; therefore, you can use all array methods on it. You can unpack rest variable data into named variables. You can give rest parameters any name, which is another major improvement over using the arguments keyword.
In the next article in the "Understanding JavaScript" series, we will introduce default parameters in JavaScript functions, so stay tuned.
Understanding JavaScript, the Rest parameter in the function, it ends here. Welcome everyone to study and discuss together:
Related reading:
The above is the detailed content of Understanding JavaScript, Rest parameters in functions. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools