Js-arguments's understanding

Source: Internet
Author: User

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
    1. var test3 = {
    2. 0:10,
    3. 1: function () {
    4. return this[0];
    5. },
    6. Length:2
    7. }
    8. 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
  1. var o = {
  2. Name:"JavaScript"
  3. };
  4. var F1 = function () {
  5. return 2;
  6. }
  7. var foo = function (a,b,c) {
  8. Console.log (arguments, typeof arguments, $.type (arguments), typeof Arguments.push);
  9. //[100, function (), object {name= "JavaScript"}] Object object undefined
  10. //Although arguments is not an array, you can still remove each element through a for loop
  11. For (var i in arguments) {
  12. Console.log (Arguments[i]);
  13. }
  14. / * Print
  15. 100
  16. function ()
  17. Object {name= "javascript"}
  18. */
  19. ///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)
  20. var ret = Array.prototype.slice.call (arguments);
  21. Console.log (ret, typeof ret, $.type (ret), typeof Ret.push);
  22. //[100, function (), object {name= "JavaScript"}] Object Array function
  23. }
  24. 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
    1. function Func (A, b) {
    2. alert (a);
    3. alert (b);
    4. For (var i=0;i<arguments.length;i++) {
    5. Alert (Arguments[i]);
    6. }
    7. }
    8. 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
    1. var sum=function (n) {
    2. if (1==n) {
    3. return 1;
    4. } Else {
    5. return n + arguments.callee (n-1);
    6. }
    7. }
    8. 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
    1. /*
    2. * Demonstrate the use of arguments, how to get real parameters and number of shapes
    3. */
    4. function Argtest (a,b,c,d) {
    5. var numargs = arguments.length;  //Gets the numeric value of the passed parameter.
    6. var expargs = argtest.length;  //Gets the value of the desired parameter.
    7. Alert ("real parameter:" +numargs)
    8. Alert ("Number of shapes:" +expargs)
    9. Alert (Arguments[0])
    10. Alert (argtest[0]) //undefined no such usage
    11. }
    12. Argtest (ON)
    13. Argtest (1,2,3,4,5)
    14. /*
    15. * Arguments not arrays (array Class)
    16. */
    17. Array.prototype.selfvalue = 1;
    18. function testaguments () {
    19. Alert ("arguments.selfvalue=" +arguments.selfvalue);
    20. }
    21. Alert ("Array.sefvalue=" +new Array (). Selfvalue); 1
    22. Testaguments (); Arguments.selfvalue = undefined
    23. /*
    24. * Demonstrates the callee property of a function.
    25. * Description: Arguments.callee: The initial value is the function object being executed for the anonymous function
    26. */
    27. function Calleedemo () {
    28. alert (Arguments.callee);
    29. }
    30. Calleedemo ();
    31. (function (ARG0,ARG1) {alert ("Number of shapes:" +arguments.callee.length)}) ();
    32. /*
    33. * Demonstrates the caller property of a function.
    34. * Description: (current function). Caller: Returns a reference to the function that called the current function
    35. */
    36. function Callerdemo () {
    37. if (callerdemo.caller) {
    38. var a= callerdemo.caller.arguments[0];
    39. alert (a);
    40. } Else {
    41. Alert ("This is a top function");
    42. }
    43. }
    44. function Handlecaller () {
    45. Callerdemo ();
    46. }
    47. Callerdemo ();
    48. Handlecaller ("Parameter 1", "Parameter 2");
    49. /*
    50. * Demonstrates the use of the Apply,call function
    51. * Description: The role is to bind the function to another object to run, the two are only defined in the way the parameters differ:
    52. * Apply (Thisarg,argarray);
    53. * Call (Thisarg[,arg1,arg2 ...]);
    54. * That is, the this pointer inside all functions will be assigned a value of Thisarg
    55. */
    56. function Objecta () {
    57. Alert ("Execute objecta ()");
    58. Alert (arguments[0]);
    59. Alert (this.b);   The value of the variable B OBJECTB when Objecta is called with call/apply in OBJECTB.
    60. this.hit=Function (msg) {alert (msg)}
    61. this.info="I'm from Objecta."
    62. }
    63. function Objectb () {
    64. Alert ("Execute OBJECTB ()");
    65. this.b = ' 2b ';
    66. //Call the Objecta () method, and all this in the Objecta constructor is replaced by this in the OBJECTB
    67. Objecta.apply (this,arguments);   Objecta.call (this);
    68. Alert (this.info);
    69. }
    70. //OBJECTB (' parameter 0 ');
    71. var value="global variable";
    72. function Obj () {
    73. this.value="Object!  ";
    74. }
    75. function Fun1 () {
    76. Alert (this.value);
    77. }
    78. //fun1 ();
    79. //fun1.apply (window);
    80. //fun1.apply (New OBJ ());

Js-arguments's understanding

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.