In the previous article we discussed the default parameters in JavaScript, and this article discusses the arguments parameter object for JavaScript.
As a function of the following example, how do we do different processing according to the different parameters that are passed in?
Copy Code code as follows:
function AddAll () {
What do we have
1.arguments is of course only meaningful in the function body, ARGUMENTS.LENGTH returns the number of arguments passed in the function.Arguments only makes sense in the function body, ARGUMENTS.LENGTH returns the number of arguments passed in the function, such as what I have not passed in here, but directly run an anonymous function, then the first alert must be
Today we look at arguments objects and properties. Arguments objects cannot be created explicitly, arguments objects are available only when the function is started. The arguments object of a function is not an array, and a single parameter is accessed in the same way as an array element. Index n is actually 0 of the
Valid JavaScript Item 24 uses a variable to save arguments references.
This series serves as the Reading Notes for objective JavaScript.
Assume that an API is required to traverse several elements, as shown below:
var it = values(1, 4, 1, 4, 2, 1, 3, 5, 6);it.next(); // 1it.next(); // 4it.next(); // 1
The corresponding implementation can be:
function valu
Of course, arguments only makes sense in the function body. arguments. length returns the number of arguments passed into the function at noon, and slightly studies the arguments in the javascript function.
The Code is as follows:
) return 1;
else return n + arguments.callee (n-1);
}
function Test () {
Alert (sum (100));; Put in HTML page for execution
function sum (n) {
if (1 = n) return 1;
else return n + arguments.callee (n-1);
}
function Test () {
Alert (sum (100));; Put in HTML page for execution
}
A1 and A2 Both answered the question, believing that the first method is common practice for most people, but JS recommends using the second, the original book says A1 this Way "where functions contain calls to sum itself,
In the function body, the identifier arguments is an application that points to the real parameter object, and the real parameter object is an array object, so that you can access the real parameter value of the input function through the numerical subscript,
Demonstrate arguments usageView code
// Query the maximum function Maxs () {VaR _ max = number. negative_infinity; // negative infinity. This valu
There are three ways in which the function functions are defined:1.function statement form; 2. The form of the direct amount of the function 3. The first two ways to create functions in the form of constructors: Function statement form, JavaScript interpreter directly to find and explain;The equivalent of the JS code to explain the function statement form; the other code executes from top to bottom.It can be understood that the first one can be called
Javascript uses the arguments parameter to disguise the method for overloading
Methods are overloaded in many object-oriented advanced languages. Javascript has no way to overload this concept. However, we can use the arguments parameter to disguise it as a function overload.
Before simulation, let's take a look at the
Many interviews may involve this arguments:Arguments people who know a little bit would think that this is an arrayArguments is a variable-length argument list, which is not an array but arrays, but also has length and can be passed subscript arguments [0] to get the first argument passed in, just like a real array.You can get the number of arguments by using the following code:function Fun (x, Y, z) {
Arguments attributes:
The arguments attribute allows the function to process variable numbers of parameters. The Length attribute of the arguments object contains the number of parameters passed to the function. For a single parameter contained in an arguments object, the access method is the same as the access metho
This example describes the arguments usage in JavaScript. Share to everyone for your reference. The specific analysis is as follows:
Let's look at the following examples:
function Add (n1,n2) {return
n1+n2;
}
function Add (n1,n2,n3) {return
n1+n2+n3;
}
Alert (Add (1,2));
The Nan,js method uses the nearest principle;
//And because the method does not pass in the N3, the result is Nan
JS
Outline
Function
Caller Returning function callers
Callee calls itself
functionfunction formatfunction Getprototynames (o,/*optional*/a) { a = a | | []; For (var p in O) { a.push (p); } return A; } CallerFunc.caller Returning function callers function Callfunc () { if (Callfunc.caller) { alert (callfu
First look at the code
functionsayHello(){
if(arguments[0]=="bye")
return;
alert(arguments[0]);
}
1. Call sayHello ('bye') and return directly without any information
2. Call sayHello ('helloworld') and prompt "HelloWorld"
Arguments also provides an attribute arguments. length, acting on the number o
JS creates an implicit object arguments when the function is called.Arguments contains the parameter array object that is actually passed to the function when the function is called.APP = {}; App.fun0 = function () { console.log (arguments)}; APP.FUN1 = function (arg1) { console.log (arguments)}; app.fun2 = function (Arg1, arg2) { console.log (
I am in the middle.
We all know that in Javascript, we can pass any number of parameters to the function, instead of the number of parameters declared by the function. Is it possible to turn a function into a function that strictly requires the number of parameters and the number of actual parameters? Naturally, it is possible. My first reaction is to check whether arguments. length is equal to its own Len
1. preface what is arguments, caller, callee? What is the role of javascript? This article will give a basic introduction to this. 2. argumentsarguments: When a function is called, a hidden object named arguments is automatically generated within the function. This object is similar to an array, but not an array. You can use the [] operator to obtain the real par
functionfoo (x, y, z) {console.info (arguments.length); //2 Actual number of parametersConsole.info (Arguments[0]);//The first parameter passed inArguments[0] = 10; Console.info (x); //10 There is a binding relationship because the first argument is passed inARGUMENTS[2] = 100; Console.info (z); //undefined does not pass in the third argument, there is no binding relationshipConsole.info (Arguments.callee = = = foo);//true}foo (The); Foo.length//
1. PrefaceArguments, caller, what is callee?What is the role in JavaScript? This article will do some basic introduction.This article was reproduced from: http://blog.csdn.net/oscar999/article/details/85113352. ArgumentsArguments: When a function is called, a hidden object named arguments is automatically generated inside the function. The object is similar to an array, but not an array. You can use the []
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.