Learning JavaScript with me arguments objects _javascript tips

Source: Internet
Author: User

1, what is arguments

Arguments is a built-in object in JavaScript, it's quirky and often overlooked, but it's really important. All the major JS libraries use the arguments object. So agruments objects must be familiar to JavaScript programmers. In the JavaScript function body, the identifier arguments has a special meaning. It is a special property of the calling object that refers to the arguments object. The Arugments object is like an array, notice that it's just like not ha.

JavaScript function body, arguments like an array (not a real array, is a arguments object, again) , has the length property, can represent the number of parameters passed to the function.

The arguments object in JavaScript is the actual parameter of the function, and the length of the arguments object is determined by the number of arguments rather than the number of parameters. A formal parameter is a variable that arguments the memory space inside a function, but it does not overlap with the memory space of the object.

JS does not take the initiative to determine for you how many parameters you have given to the function, if you pass more, the superfluous part is not used, if you less pass, Then the parameter value that is not transmitted is undefined. So we can use the length property of arguments to detect whether the correct number of actual parameters are used when calling the function, because JavaScript doesn't do it for you.

function f (x,y,z)
{
 ///first check that the number of arguments 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 a variable parameter list function

Arguments also provides us with the possibility of passing any number of actual arguments to a function:

For example, I would like to use a display () function to calculate the total payroll of each company, yes, you can pass the number of parameters, but only if you want to pass the numbers, because I am lazy in the function to judge.

 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);

What do you think? This is a clever method, isn't it?

Explain that arguments is consistent with the formal parameters of the actual transmission:

For both arguments and values, the values are synchronized to change one of the values, that is, the value of both

function f (A, B, c) {
 alert (arguments.length);//Result: "2"
 a = m;
 Alert (arguments[0]);  Result: "arguments[0"
 = "Qqyumidi";
 alert (a);     Result: "Qqyumidi"
 alert (c);     Result: "undefined"
 C =;
 Alert (arguments[2]);  Result: "Undefined"
}

F (1, 2);

3, never modify the arguments object

The connection between the arguments declared in a function and the arguments is fragile, and each declared argument is actually a reference to the corresponding position in the arguments object.

It is noteworthy that in ES5 's strict mode, the parameters declared by the function do not refer to the 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 of the inconsistent relationship between the parameters of function declarations and arguments in strict and strict modes, it is safest to avoid problems by modifying arguments objects.

If you do 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, it performs a copy operation, and the resulting args is also a true array object. Also, there is no connection between the parameters of the args and the function declaration, and it is safe to operate on it.

4, a variable to save the arguments reference

Suppose you need an API to traverse several elements, as follows:

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 n EW Error ("End of Iteration"); 
   } 
   return arguments[i++]; Wrong arguments}} 
 

But the reality 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 for this is that the assignment to the arguments object is implicitly done. Inside the next method, arguments is used, but the arguments at the beginning of this arguments and values method is not an object. The arguments object here is the function next ().

The workaround is also simple: use a different variable to refer to the arguments you need to access. It is then possible to access the properties of the closure in its nested functions, as follows:

function values () { 
 var i = 0, n = arguments.length, a = arguments; 
 return { 
  hasnext:function () {return 
   i < n; 
  }, 
  next:function () { 
   if (i >= N) { 
    throw new E Rror ("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 Arguments object callee properties:

The arguments Callee property is used to refer to the currently executing function, which is very beneficial to the unnamed function call itself.

A recursive function is first implemented with a named function expression:

Function Direct quantity specifies the 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 the function can be directly measured by the function name. In this way the realization of recursion can be very convenient to call themselves.

Now with the arguments this callee can also be simple to achieve

Using the direct quantity of the function, using Arguments.callee attribute to realize the recursive function
var result = function (x) {
 if (x<=1) return 1; 
 Return X*arguments.callee (x-1);
};

At the end of the note, since this arguments is so powerful, let's not name the variable arguments, in fact arguments is one of the reserved words for JavaScript. Well.

Finally, add one point:

Difference caller

Returns a reference to a function that calls the current function.

    • -Functionname.caller
    • -The FunctionName object is the name of the function being executed.

For functions, the caller property is only defined when the function executes. If the function is called by the top level, then caller contains null. If you use the Caller property in the string context, the result is the same as functionname.tostring, that is, the inverse compiled text of the function is displayed.

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 was a top function");
 }
function Handlecaller () {
 callerdemo ();
}

Handlecaller ()//popup Handlecaller definition

The above is for JavaScript arguments object of the relevant introduction, hope for everyone's learning help.

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.