Arguments and formal parameters tutorial in JavaScript

Source: Internet
Author: User
Tags numeric numeric value

There is a arguments in the function default: he represents the parameter that the function passes in when it executes

Arguments: arguments, actual parameters

Arguments: Variable parameters refer to the number of arguments that are not fixed.
Although it is not necessary to pass a value through a formal parameter, if the value of the formal parameter is modified, the value in the corresponding position in the arguments is also modified.

Code fragment

function count (a,b,c,d) {

alert (c);

return a+b+c+d;
}
Alert (count (1,2,4,5)); 12
Alert (count (1,2,3,4)); 10

Code fragment

function count (a) {//parameter: Formal parameters


Arguments: There is a collection of all the actual parameters that are actually passed in.

return arguments.length;

return arguments[1];

A = 10;

var results = 0;

for (var i=0; i<arguments.length; i++) {

Results + = Arguments[i];

Results = results + arguments[i];

}

0 + 1 + 4 + 3 + 10 + 20

return results;
}

Alert (count (1,4,3,10,20)); Real parameter 47

Alert (count (1,2)); 12

Alert (count (1,4,3)); 6 0+1+4+3

Alert (count (1,2,3,4)); 10 0+1+2+3+4

The following example shows the use of a arguments object in JavaScript.

function Argtest (A, b) {
var i, S = "The Argtest function expected";
var Numargs = arguments.length; Gets the numeric value of the parameter being passed.
var Expargs = argtest.length; Gets the numeric value of the expected parameter.
if (Expargs < 2)
S + + Expargs + "argument."
Else
S + + Expargs + "arguments."
if (Numargs < 2)
S + + Numargs + "was passed."
Else
S + + Numargs + "were passed."
s + = "\ n"
For (i =0 i < Numargs; i++) {//Get parameter content.
S + + "ARG" + i + "=" + arguments[i] + "\ n";
}
return (s); Returns a list of parameters.
}

When a formal parameter is defined as a method, the method carries a parameter rather than a method that now
public void Printinfo (String info) {
SYSTEM.OUT.PRINTLN (info);
}
Here info is a formal parameter, which is of type string.
An argument is a parameter value that you pass to the method when you invoke the method, such as a statement:
**.printinfo ("Hello");(here * * Represents an object of the class where the Printinfo method is located), where "Hello" is an argument, and when the method invocation is implemented, the system will give the parameter info variable the value of the argument "Hello". That is, info points to "Hello", and when this method is invoked, it prints out on the screen hello

Convert function argument arguments to array

1. Why is arguments not an array? How to prove it?

Arguments is not a slice of arrays, and so is not an array type.

Verify:

function Testargs () {
var arr=[1,2,3];
Console.log (typeof Arguments.slice);
Console.log (typeof Arr.slice); }
Testargs ();

Output:

Undefined function

Of course, there are other ways to prove it, such as watching constructor and so on.

2. How do I convert to an array?

Use the slice method of the array as follows:

function Arg2arr ()
{
var arr = Array.prototype.slice.call (arguments);
Console.log (arr);
}
Arg2arr (1,2,3);

Output: [1,2,3]

It can also be written like this:

Array.prototype.slice.call (arguments, 0)

3. How to use native JS to implement your own slice method

Array.prototype.slice = function (start,end) {
var result = new Array ();
Start = Start | | 0;
End = End | | This.length; This refers to the object being invoked, which, when called, can change the point of this, which is to point to the incoming object, which is the key

for (var i = start, I < end; i++) {Result.push (this[i]);  return result; }

4. Is there ToArray method in JS? If not, how do you implement one yourself?

var ToArray = function (s) {
try{
return Array.prototype.slice.call (s);
catch (e) {
var arr = [];
for (var i = 0,len = S.length i < len; i++) {
Arr.push (S[i]);
Arr[i] = s[i]; It's said to be faster than push.
}
return arr;
}
}

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.