Understanding of js-arguments

Source: Internet
Author: User
Understanding of js-arguments: Construct an Array of Arraylike classes by yourself. Key points: 1. The element is an Array index 2. You can construct an Array like by yourself with the length attribute.
Key points of the class array:
1. The element is an array index.
2, with the length attribute

Js Code

Var test3 = {0: 10, 1: function () {return this [0] ;}, length: 2} console. log (Array. prototype. slice. call (test3); // print [10, function ()]

The function parameter arguments is a class array, but not an array. (Arguments is an object)
If you want to convert it to a real array, you can
Var slice = []. slice;
Slice. call (arguments );
You can also: Aarray. prototype. slice. call (arguments );

Js Code

Var o = {name: "javascript"}; var f1 = function () {return 2;} var foo = function (a, B, c) {console. log (arguments, typeof arguments, $. type (arguments), typeof arguments. push); // [100, function (), Object {name = "javascript"}] object undefined // although arguments is not an array, however, you can still retrieve each element for (var I in arguments) {console. log (arguments [I]);}/* print 100 function () Object {name = "javascript"} * // now let the slice of the array run in the context of arguments, if the parameter is 0, arguments is converted to an Array (arguments itself has not changed) var ret = Array. prototype. slice. call (arguments); console. log (ret, typeof ret, $. type (ret), typeof ret. push); // [100, function (), Object {name = "javascript"}] object array function} foo (100, f1, o );

Basic Introduction
In addition to the specified parameters, js also creates an implicit object, arguments.
1. arguments can be set using a syntax such as arguments [index]. It has the length attribute. The arguments object stores the parameters actually passed to the function, not limited to the list of parameters defined in the function declaration. For example:

Js Code

Function func (a, B) {alert (a); alert (B); for (var I = 0; I

The code is displayed as follows: 1, 2, 2, 3, 4. The function defines two parameters, but four parameters are passed during the call.

Ii. callee attribute of arguments
It refers to the reference to the function object, which is conducive to recursion of the unknown function or to ensure function encapsulation. For example, recursion is used to calculate the sum of Natural Numbers 1 to n:

Js Code

var sum=function(n){      if(1==n) {          return 1;      } else {          return n + arguments.callee(n-1);      }  }  alert(sum(100));

Arguments. callee indicates to reference the currently executed function, or to reference the function object that calls arguments. callee. It provides a self-reference method for anonymous functions.

More in-depth, including caller, call, and apply

Js Code

/** Demonstrate arguments usage. How to obtain real parameters and number of shapes */function argTest (a, B, c, d) {var numargs = arguments. length; // obtain the value of the passed parameter. Var expargs = argTest. length; // obtain the value of the expected parameter. Alert ("real parameter quantity:" + numargs) alert ("parameter quantity:" + expargs) alert (arguments [0]) alert (argTest [0]) // undefined does not have this usage} // argTest () // argTest (, 5)/** arguments is not an Array (Array class) */Array. prototype. selfvalue = 1; function testAguments () {alert ("arguments. selfvalue = "+ arguments. selfvalue);} // alert ("Array. sefvalue = "+ new Array (). selfvalue); // 1 // testAguments (); // arguments. selfvalue = undefined/** demonstrate Function c Allee attribute. * Description: arguments. callee: the initial value is the Function object being executed. It is used for anonymous functions */function calleeDemo () {alert (arguments. callee);} // calleeDemo (); // (function (arg0, arg1) {alert ("Number of shapes:" + arguments. callee. length)}) ();/** demonstrate the caller attribute of the function. * Description: (current function ). caller: returns a reference to the function, which calls the current function */function callerDemo () {if (callerDemo. caller) {var a = callerDemo. caller. arguments [0]; alert (a);} else {alert ("this is a top function") ;}} Function handleCaller () {callerDemo () ;}// callerDemo (); // handleCaller ("parameter 1", "parameter 2 "); /** demonstrate the use of the apply and call functions * Description: The function is bound to another object for running. The two functions are different only when defining parameters: * apply (thisArg, argArray); * call (thisArg [, arg1, arg2…] ]); * That is, the this pointer inside all functions will be assigned to thisArg */function ObjectA () {alert ("execute ObjectA ()"); alert (arguments [0]); alert (this. b); // The value of ObjectB variable B when calling ObjectA with call/apply in ObjectB. This. hit = function (msg) {alert (msg)} this.info = "I'm from ObjectA"} function ObjectB () {alert ("execute ObjectB ()"); this. B = '2b '; // call the ObjectA () method. At the same time, all this in the ObjectA constructor will be replaced by this in ObjectB. apply (this, arguments); // ObjectA. call (this); alert (this.info);} // ObjectB ('0' parameter); var value = "global variable"; function Obj () {this. value = "object! ";}Function Fun1 () {alert (this. value) ;}// Fun1 (); // Fun1.apply (window); // Fun1.apply (new Obj ());

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.