Home>

Learn the arguments object of javascript,

1.What are arguments

arguments is a built-in object in javascript,It's weird,Often overlooked,But it's actually important.The arguments object is used by all major js libraries. Therefore, the agruments object must be familiar to JavaScript programmers.In the body of the javascript function,The identifier arguments have a special meaning.It is a special property of the calling object,Used to reference the arguments object. arugments objects are like arrays,Note that this is just not like Ha.

javascript function body,arguments like array(Not really an array,Is an arguments object, again emphasized)Likewise, there is a length attribute, which can represent the number of parameters passed to the function.

The arguments object in JavaScript is the actual parameter of the function,The length of the arguments object is determined by the number of arguments, not the number of formal parameters.The parameter is a variable that re-opens the memory space inside the function.But it does not overlap with the arguments object memory space.

js will not actively determine for you how many parameters you pass to the function,If you pass more,The extra parts are not used,If you pass less,Then the parameter value that is not passed is undefined. So we can use the length property of arguments to detect whether the correct number of actual parameters were used when calling the function.Because javascript doesn't do these things for you

function f (x, y, z)
{
 //First check if the number of parameters passed is correct
 if (arguments.length!=3)
 {
  throw new error ("function f called with" + arguments.length + "arguments");
 }
 //Run the real function below
}

2.arguments create variable parameter list function

arguments also provide us with such a possibility,Just pass any number of actual arguments to a function:

For example, I want to use a display () function to calculate the total salary of employees in each company.Yes, yes, you can pass as many parameters as you like.But the premise is that you need to pass numbers,Because I'm too lazy to judge inside the function.

function display () {
  var sum=0;//Total
  for (var i=0;i<arguments.length;i ++) {
   sum +=arguments [i];
  }
  document.write (sum + "<br>");
 }
 //a company
 display (10000,2000,5000);
 //b company
 display (1000, 2000, 5000, 8000, 10000);

how about it?This method is clever, right?

Explain that arguments are consistent with the formal parameters passed:

For both arguments and values,Both values ​​change one of them synchronously,I.e. change all the values ​​of both

function f (a, b, c) {
 alert (arguments.length);//result:"2"
 a=100;
 alert (arguments [0]);//result:"100"
 arguments [0]="qqyumidi";
 alert (a);//result:"qqyumidi"
 alert (c);//result:"undefined"
 c=2012;
 alert (arguments [2]);//result:"undefined"
}
f (1, 2);

3. Never modify the arguments object

The link between arguments declared in functions and arguments is weak,Each declared parameter is really just a reference to the corresponding position in the arguments object.

It is worth noting thatIn strict mode of es5, arguments to function declarations do not reference arguments:

function strict (x) {
 "use strict";
 arguments [0]="modified";
 return x === arguments [0];
}
function nonstrict (x) {
 arguments [0]="modified";
 return x === arguments [0];
}
strict ("unmodified");//false
nonstrict ("unmodified");//true

Because in strict and non-strict modes, the relationship between the parameters of a function declaration and arguments is inconsistent,So to avoid problems,It is safest not to modify the arguments object.

If you really need to modify the arguments object, you can first assign a arguments object:

var args=[] .slice.call (arguments);

When the slice method does not accept any parameters,Will perform the copy operation,The resulting args is also a real array object.At the same time, there is no connection between args and the parameters of the function declaration.It is safe to operate it.

A variable to hold references to arguments

Suppose you need an api to iterate over several elements.Like this:

var it=values ​​(1, 4, 1, 4, 2, 1, 3, 5, 6);
it.next ();//1
it.next ();//4
it.next ();//1

The corresponding implementation can be:

function values ​​() {
 var i=0, n=arguments.length;
 return {
  hasnext:function () {
   return i<n;
  },  next:function () {
   if (i>= n) {
    throw new error ("end of iteration");
   }
   return arguments [i ++];//wrong arguments
  }
 };
}

But the actual situation of implementation is:

var it=values ​​(1, 4, 1, 4, 2, 1, 3, 5, 6);
it.next ();//undefined
it.next ();//undefined
it.next ();//undefined

the reason is:The assignment to the arguments object is done implicitly.Inside the next method,Arguments are used, however the arguments at the beginning of the arguments and values ​​methods are not an object.The arguments object here is the function next ().

The solution is also very simple,Just refer to the arguments that need to be accessed using another variable.Then you can access it in its nested function through the nature of the closure,Like this:

function values ​​() {
 var i=0, n=arguments.length, a=arguments;
 return {
  hasnext:function () {
   return i<n;
  },  next:function () {
   if (i>= n) {
    throw new error ("end of iteration");
   }
   return a [i ++];
  }
 };
}
var it=values ​​(1, 4, 1, 4, 2, 1, 3, 5, 6);
it.next ();//1
it.next ();//4
it.next ();//1

5. The callee property of the arguments object:

The callee attribute of arguments is used to refer to the currently executing function,This is great for unnamed function calls themselves.

First implement a recursive function with a named function expression:

//Function direct quantity Specify function name Recursive function
var result=function fact (x) {
 if (x<= 1)
  return 1;
 else
  return x * fact (x-1);
};

In that, I mentioned that you can directly assign a function name to a function.In this way, recursion can be called easily.

Now this callee with arguments can also be easily implemented

//Directly measure with functions,Implementing recursive functions using the arguments.callee property
var result=function (x) {
 if (x<= 1) return 1;
 return x * arguments.callee (x-1);
};

Let me remind you at the end,Since these arguments are so powerful,Then we don't name the variables arguments, in fact arguments are one of the reserved words of javascript.Ok.

One last addition:

Difference caller

Returns a reference to a function,This function calls the current function.

-functionname.caller -The functionname object is the name of the function being executed.

For functions,The caller attribute is only defined when the function is executed.If the function is called from the top level,Then the caller contains null. If the caller attribute is used in a string context, the result is the same as functionname.tostring, that is,The decompiled text of the function is shown.

Code:

code highlighting produced by actipro codehighlighter (freeware) http://www.codehighlighter.com/-->//caller demo {
function callerdemo () {
 if (callerdemo.caller) {
  var a=callerdemo.caller.tostring ();
  alert (a);
 } else {
  alert ("this is a top function");
 }
}
function handlecaller () {
 callerdemo ();
}
handlecaller ();//The definition of handlecaller popup
  • Previous Take the time today to sort out the relevant knowledge of jquery and ajax
  • Next Learn with me the basic types and reference types of javascript