Home > Article > Web Front-end > What are arguments in js? How to use arguments in js
The content of this article is about what are arguments in js? The usage of arguments in js has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Array-like objects: arguments
As we all know, js is a very flexible language. When we call a function in js, we often pass some parameters to the function. js stores all the parameters passed into the function in something called arguments, so what exactly is this? What is it?
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.
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:
##Finally we can also see that there is another argument calledcallee’s attribute, this attribute represents a reference to the current function. To put it simply, the code of the function we call stored in this attribute is really incomprehensible, and it’s time for console.log to show its talents. It's time.
Finally, we can also see that arguments also have an attribute called callee. This attribute represents a reference to the current function. To put it simply, this attribute stores the code of the function we call. When I really can't understand it, it's time for console.log to show its talents.function showcallee() { var a = '这里是代码'; var b = '这是另一段代码'; var c = a + b; console.log(arguments.callee); return c; } showcallee();Are you as shocked as me when you see the result? Isn’t this the code I wrote? arguments.callee completely completes this function This code returns some wonderful uses of
arguments
1. Use arguments to implement overloading of methods
The following We use the arguments object to implement a function that adds parameters. No matter how many parameters are passed in, it 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) ); //6 console.log( add(1,3) ); //4 console.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 this function.
function factorial(num) { if(num<=1) { return 1; }else { return num * arguments.callee(num-1); } }Although this method is easy to use, there is one thing worth noting. In order to limit the flexibility of js and make js strict, a new strict mode is added in ECMAScript 4. In strict mode, we are prohibited from using var. To directly declare a global variable, of course this is not the point. The point is that the arguments.callee attribute is also prohibited. But this is not a problem. ES6 has added many useful variable declaration methods and new syntax sugar for us. As a fashionable front end, let’s quickly learn some ES6’s new syntax. Related recommendations:
What are the implementation methods of JS modularization? Explanation of js modularization
#What is a js object? What are the js object types? Summary of js object types
The above is the detailed content of What are arguments in js? How to use arguments in js. For more information, please follow other related articles on the PHP Chinese website!