Home > Web Front-end > JS Tutorial > Detailed explanation of arguments in js

Detailed explanation of arguments in js

小云云
Release: 2018-03-14 16:52:15
Original
1558 people have browsed it

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);
Copy after login

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/>
Copy after login

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? <br/> 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 &#39;传入参数不合法&#39;;
    }
}

console.log( add(2,3) );
console.log( add(1,2,3) );
Copy after login

Look at the results:

Detailed explanation of arguments in js

##Finally we can also see that there is another argument called callee Attribute, this attribute represents a reference to the current function. To put it simply, when the code of the function we call stored in this attribute is really incomprehensible, it is time for console.log to show its talents.

function showcallee() {
    var a = &#39;这里是代码&#39;;    var b = &#39;这是另一段代码&#39;;    var c = a + b;

    console.log(arguments.callee);    return c;
}
showcallee();
Copy after login

Detailed explanation of arguments in js

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 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) );   
 //26
Copy after login

Since 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 factorials

function factorial(num) { 
    if(num<=1) { 
        return 1; 
    }else { 
        return num * factorial(num-1); 
    } 
}
Copy after login

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); 
    } 
}
Copy after login

这个方法虽然好用,但是有一点值得注意,ECMAScript4中为了限制js的灵活度,让js变得严格,新增了严格模式,在严格模式中我们被禁止不使用var来直接声明一个全局变量,当然这不是重点,重点是arguments.callee这个属性也被禁止了。不过这都不是事儿,ES6为我们新增了很多好用的变量声明方式和新的语法糖,作为一个时髦的前端,我们赶紧学习一些ES6的新语法吧。

相关推荐:

js中arguments.length解析

js函数的调用及有关隐式参数arguments和this的问题

JavaScript arguments 对象详解

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template