The first question
 var length =10;
 function fn() {
     console.log( this.length );
 }
 ​
 var obj = {
     length: 5 ,
     method: function (fn) {
         fn();
         arguments[0] (); } } obj.method(fn ,1)
Copy the code

parsing

  • First, we define a variable length, an object obj, and a function fn globally, with length assigned to 10.
  • Next, the fn function prints this.Length
  • In obj, obj.length is 5 and obj.method is a function. Method (fn); arguments (fn); method (fn)
  • So arguments[0]() represents the first item to call arguments.
  • Method (fn,1) refers to calling method in obj with two arguments, fn and 1
  • After analyzing the meaning of the code, we look at the output.
  • The fn function called in method is a global function, so this refers to window, this.length = 10.
  • Arguments0 (arguments0); arguments0 (arguments0); arguments0 (arguments0); arguments0 (arguments0); arguments0 (arguments0);
  • The final output is 10, 2

The second question

 function a(xx) {
     this.x = xx;
     return this;
 };
 ​
 var x = a(5);
 var y = a(6);
 ​
 console.log(x.x);
 console.log(y.x);
Copy the code

parsing

  • First, we globally define a variable x, a variable y, and a function A. Function A in which this.x is equal to the received argument and returns this.
  • Next we assign x to a(5) and y to a(6). Finally, we output x.x, y.x.
  • After analyzing the meaning of the code, let’s look at the output
  • Function A passes 5, so this.x is assigned 5. This refers to window, i.e. Window. x = 5.
  • X =a(5) is the same as window.x =window, and x is assigned to window.
  • Then y = a(6) is executed, that is, x is changed again to 6, and y is assigned to window.
  • Console. log(x.x) is equivalent to
  • Console. log(6.x), the output is undefined.
  • Console. log(y.x), the output is equivalent to
  • Console. log(window.x), of course, gets the value 6
  • So the final output is undefined 6