Javascript (arguments)

Source: Internet
Author: User

Cause

Today I went to The Kingsoft network to recruit interns. I was a bit uncertain when I had a question. After all, my head was notJavascript parserAnd then you can open the chrome console. After running the SDK, you feel that you have to summarize this knowledge point.

The question is as follows (I can only say it is rough, a little forgotten, but the knowledge points are the same)
  • Function foo (a, B, c ){
  • C = 3;
  • Console. log (arguments );
  • }
  • Foo (1, 2)
  • Foo (1, 2, undefined)

    When I saw this question at the time, I still had a lot of questions in my mind. At last, I had to sum it up as a arguments object. When did I assign a value?

    Understanding the basic information of argumentsarguments 1. It is not a real array, but an object of A Class array. What is the difference between the two? This is not the focus of our article; 2. It is created in the function definition, set to null, and assigned to the function call. It is a reference to the function parameter list, is the function body access parameter Interface 3, it can only act on the function body of the function, in the external access to this object will be wrong or only nullarguments wonderful Behavior 1 because it is an object of the class array, it has a length attribute and can use subscript to access the parameter list. 2 because it is a function attribute, if you re-define an arguments variable in the function body, it will overwrite the original arguments.

    Code:

    • Function foo (a, B, c ){
    • Var arguments = 1;
    • Console. log (arguments );
    • }
    • Foo (1, 2)

      Result: 1

      3. In non-strict cases, we can use arguments to modify the parameter list.

      Code:

      • Function foo (a, B, c ){
      • Arguments [0] = "monkindey ";
      • Console. log ();
      • }
      • Foo (1, 2)

        Result: "monkindey"

        Well, let's reveal some of the questions about Kingsoft intern recruitment.

        Foo (1, 2) Answer: [1, 2]
        Cause: the arguments identifier pointed out in the javascript authoritative guide that it is a reference to a real parameter object. The real parameter object is an array of classes. When the real parameter object is changed, the arguments will also be changed. The real parameter object is assigned a value only when the function is called. For example, the above real parameter object is similar to {a: 1, B: 2}, but there is no c, so assign a value to c, the real parameter object cannot be changed, but arguments is its reference, so it has no effect. Foo (1, 2, undefined) Answer: [1, 2, 3]
        Cause: the real parameter object will be {a: 1, B: 2, c: undefined}, and if you modify c in the function body, it will set c in the real parameter object to 3. Therefore, the console. log (arguments) is changed to [, 3], and then the local variables declared in the training function are the same as the shape parameters.
        • Function foo (a, B, c ){
        • Var a = 3;
        • Console. log (arguments [0]);
        • }
        • Foo (1, 2) Answer: 3
          Cause:I personally thinkWhen we call a function, the input real parameter is to assign a value to the form parameter in a function. In fact, it is equivalent to defining a local variable in the function. The variable name is the form parameter name. The above code can be seen
          • Function foo (a, B, c ){
          • Var a = 1; // The input parameter is equivalent to defining a local variable in the function body.
          • Var a = 3;
          • Console. log (arguments [0]);
          • }
          • Foo (1, 2) If only partial variable a is declared, but no value is assigned, the situation is different.
            • Function foo (a, B, c ){
            • Var;
            • Console. log (arguments [0]);
            • }
            • Foo (1, 2) Answer: 1
              Cause: the above Code can be viewed
              • Function foo (a, B, c ){
              • Var a = 1; // The input parameter is equivalent to defining a local variable in the function body.
              • Var a; // define a variable repeatedly without assigning values. The javascript parser will ignore it (my conjecture)
              • Console. log (arguments [0]);
              • }
              • Foo (1, 2) References

                Arguments MDN
                Javascript authoritative guide
                Modifiable JavaScript function parameters

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.