When we call a function in js, we often pass some parameters to the function. js stores all the parameters passed to the function in something called arguments. So what exactly is this? ?
Everything in js is an object, even array and string functions are objects. So this thing called arguments is also an object, and it is a special object. Its attribute name is based on the sequence of the parameters passed in. The attribute name of the first parameter is '0', and the attribute name of the second parameter is is '1', and so on, and it also has a length attribute, which stores the number of function parameters currently passed in. Many times we call this kind of object an array-like object. Array-like objects and arrays are both born from objects, but arrays are the older brother and have many more toys (methods) than array-like objects. Array-like objects are just the younger brothers that look a lot like arrays.
Wait a minute, didn’t I just say that arrays are also objects? What is this array-like object now? There is no way, js is so flexible. This array-like object not only stores the parameters passed to the function, but also has some other attributes, which will be discussed one by one later.
Because array-like objects and arrays have a lot in common, we can often use the call method to let the array-like object also use some of the methods of the array, which is to let the younger brother play with his older brother's toys, such as... , let’s not go too far, this article just talks about arguments. If you want to know more about how objects borrow array methods, please refer to this article.
Attributes of arguments
Next let’s take a look at what’s in the arguments object, whether it’s a mule or a horse.
function showargs() { console.log( arguments ); } showargs(1,2,3,4,5);
Below we use the console.log method to output the arguments object to the console. I have to say here that chrome’s console tool is extremely easy to use (I am not here to advertise).
<br/>
Here we can see that the arguments object stores the five parameters I passed in in the form of an array, and also saves the number (length) of the actual parameters I passed into the function. And we can see that ==_ proto _== of the arguments object points to object, which also shows that it is an array-like object, not an array.
With this object, when we write functions in the future, we don’t need to specify parameter names for all formal parameters, and then obtain parameters through parameter names. We can directly use the arguments object to obtain actual parameters, so Isn’t it a lot more convenient?
In some languages, after we specify the parameter name for the function, when the function is called, it will be judged whether the currently passed in parameters are equal to the number of parameters defined by the function. If not, an error will be reported, but flexible js (not me Say, js is really flexible) and will not verify whether the number of parameters passed to the function is equal to the number of parameters defined by the function. So in order to show off (the simplicity of the code), we use arguments to call parameters so as not to confuse the parameter names between different functions. In addition, in order to show off (the strictness of the code), we can also use arguments to determine whether the current number of parameters passed in is consistent with the number we need.
Here is an example:
function add() { if( arguments.length == 2 ){ return arguments[0] + arguments[1]; }else{ return '传入参数不合法'; } } console.log( add(2,3) ); console.log( add(1,2,3) );
Look at the results:
function showcallee() { var a = '这里是代码'; var b = '这是另一段代码'; var c = a + b; console.log(arguments.callee); return c; } showcallee();
Some wonderful uses of arguments
1. Use arguments to implement method overloading
Below we use the arguments object to implement a parameter relationship The adding function, no matter how many parameters are passed in, will be returned after adding the parameters passed in.function add() { var len = arguments.length, sum = 0; for(;len--;) { sum += arguments[len]; } return sum; }console.log( add(1,2,3) ); //6console.log( add(1,3) ); //4console.log( add(1,2,3,5,6,2,7) ); //26Since js is a weakly typed language, there is no overloading mechanism. When we rewrite a function, the original function will be directly overwritten. Here we can use arguments to determine the actual parameters passed in. Perform different operations on types and quantities and return different values.
2. Use arguments.callee to implement recursion
Let’s first take a look at how we implemented recursion before. This is a function that settles factorialsfunction factorial(num) { if(num<=1) { return 1; }else { return num * factorial(num-1); } }But when this function becomes an anonymous function, we can use callee to recurse the function.
function factorial(num) { if(num<=1) { return 1; }else { return num * arguments.callee(num-1); } }
这个方法虽然好用,但是有一点值得注意,ECMAScript4中为了限制js的灵活度,让js变得严格,新增了严格模式,在严格模式中我们被禁止不使用var来直接声明一个全局变量,当然这不是重点,重点是arguments.callee这个属性也被禁止了。不过这都不是事儿,ES6为我们新增了很多好用的变量声明方式和新的语法糖,作为一个时髦的前端,我们赶紧学习一些ES6的新语法吧。
相关推荐:
js函数的调用及有关隐式参数arguments和this的问题
The above is the detailed content of Detailed explanation of arguments in js. For more information, please follow other related articles on the PHP Chinese website!

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.


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.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
