Js-arguments's understanding
Blog Category:
- Web Front end-js language core
Arguments
Zccst
2014-6-10 construct an array like
The constituent points of an array of classes:
1, the element is an array-indexed
2, with the length property
JS Code
- var test3 = {
- 0:10,
- 1: function () {
- return this[0];
- },
- Length:2
- }
- Console.log (Array.prototype.slice.call (test3)); //print [Ten, Function ()]
2014-6-9
The function parameter arguments is an array of classes, but not an array. (Arguments is an object)
If you want to turn into a true array, you can
var slice = [].slice;
Slice.call (arguments);
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 object undefined
- //Although arguments is not an array, you can still remove each element through a for loop
- 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, the parameter is 0, then the arguments is turned into an array, (arguments itself does not change)
- 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
When JS makes a function call, it creates an implied object--arguments in addition to the specified parameters.
One, arguments can use the arguments[index] such syntax to take the value, has the length of the property lengths. The arguments object stores parameters that are actually passed to the function, not limited to the list of parameters defined by the function declaration, for example:
JS Code
- function Func (A, b) {
- alert (a);
- alert (b);
- For (var i=0;i<arguments.length;i++) {
- Alert (Arguments[i]);
- }
- }
- Func (1,2,3,4);
When the code runs, it is displayed in turn: 1,2,1,2,3,4. The function defines two parameters, but passes 4 arguments at the time of the call.
Second, the callee attribute of arguments
It represents a reference to the function object itself, which facilitates recursion of the nameless function or guarantees the encapsulation of the function. For example, the sum of natural numbers from 1 to n is calculated by recursion:
JS Code
- var sum=function (n) {
- if (1==n) {
- return 1;
- } Else {
- return n + arguments.callee (n-1);
- }
- }
- Alert (sum (100));
Arguments.callee represents a reference to a function that is currently executing, or a function object that invokes Arguments.callee, which provides an anonymous function with a way of self-referencing.
More in-depth, including caller, call, apply
JS Code
- /*
- * Demonstrate the use of arguments, how to get real parameters and number of shapes
- */
- function Argtest (a,b,c,d) {
- var numargs = arguments.length; //Gets the numeric value of the passed parameter.
- var expargs = argtest.length; //Gets the value of the desired parameter.
- Alert ("real parameter:" +numargs)
- Alert ("Number of shapes:" +expargs)
- Alert (Arguments[0])
- Alert (argtest[0]) //undefined no such usage
- }
- Argtest (ON)
- Argtest (1,2,3,4,5)
- /*
- * Arguments not arrays (array Class)
- */
- Array.prototype.selfvalue = 1;
- function testaguments () {
- Alert ("arguments.selfvalue=" +arguments.selfvalue);
- }
- Alert ("Array.sefvalue=" +new Array (). Selfvalue); 1
- Testaguments (); Arguments.selfvalue = undefined
- /*
- * Demonstrates the callee property of a function.
- * Description: Arguments.callee: The initial value is the function object being executed for the anonymous function
- */
- function Calleedemo () {
- alert (Arguments.callee);
- }
- Calleedemo ();
- (function (ARG0,ARG1) {alert ("Number of shapes:" +arguments.callee.length)}) ();
- /*
- * Demonstrates the caller property of a function.
- * Description: (current function). Caller: Returns a reference to the function that called 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");
- /*
- * Demonstrates the use of the Apply,call function
- * Description: The role is to bind the function to another object to run, the two are only defined in the way the parameters differ:
- * Apply (Thisarg,argarray);
- * Call (Thisarg[,arg1,arg2 ...]);
- * That is, the this pointer inside all functions will be assigned a value of Thisarg
- */
- function Objecta () {
- Alert ("Execute objecta ()");
- Alert (arguments[0]);
- Alert (this.b); The value of the variable B OBJECTB when Objecta is called 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, and all this in the Objecta constructor is replaced by this in the OBJECTB
- Objecta.apply (this,arguments); Objecta.call (this);
- Alert (this.info);
- }
- //OBJECTB (' parameter 0 ');
- var value="global variable";
- function Obj () {
- this.value="Object! ";
- }
- function Fun1 () {
- Alert (this.value);
- }
- //fun1 ();
- //fun1.apply (window);
- //fun1.apply (New OBJ ());
Js-arguments's understanding