A question about the direction of javascript this

A question about javascript pointer for detailed solution and analysis.

var length = 10;
function fn(){
    alert(this.length)
}
var obj = {
    length: 5,
    method: function(fn){
        fn() // ?
        arguments[0]() // ?
    }
}

obj.method(fn)
Feb.27,2021

var length = 10;
function fn(){
    alert(this.length)
}
var obj = {
    length: 5,
    method: function(fn){
        fn() // 10  window
        arguments[0]() // 1  arguments
    }
}
obj.method(fn)

first question: fn () points to this knowledge point:

if a non-arrow function is nested in any function, then the this in the nested function should point to the window object
without being specified.

second question: arguments [0] () points to this knowledge point:

in a method call (if the property of an object is a function, this property is called a method, and calling this property is called a method call), when the body of the function is executed, the object and array that act as the access body of the property is the point of the this in the calling method. (generally speaking, whoever calls the method this will point to him;)

this place may be confusing. arguments [0] points to fn , so arguments [0] () invokes fn as an attribute of arguments object [0] , so this in fn points to the object arguments of the attribute access subject; < / p

give an example:

[function fn(){alert(this.length)}][0]()//1
The

array is also an object, except that the integer attribute of the array object is counted in the length attribute and is treated differently. Here, the 0 attribute of the array object is called, and the function is called as the attribute of the array object. The this in the function points to the array, of course, so it returns the length of the array, of course

.

other detail stamps pointing to this in js


var length = 10;
function fn() {
  console.log('fn:', this);
  console.log('fn:', this.length);
} 
var obj = {
  length: 5,
  method: function(fn) {
    fn() // ?
    console.log(arguments[0]);
    arguments[0](); // ?
  }
};

obj.method(fn);

this will be clearer. The first fn () has no object context, so when calling fn () directly, its this points to the global window or process , so the printed length is global, that is, 10 . The second arguments [0] () has the object context arguments , and its this points to arguments , so the printed length is actually arguments.length , that is, 1 ,

.

if you put this code directly in the script tag in html, length is the global variable, and you can see length under windows.

var length = 10;
function fn(){
    alert(this.length)
}
var obj = {
    length: 5,
    method: function(fn){
        fn() // windowslength 10
        arguments[0]() // argumentslength1fn thisarguments1.
    }
}

the first direct call to fn,this points to window. The second arguments [0] (), is equivalent to an object calling one of its own property functions (similar to the this in the obj.fn (), fn function pointing to obj. So the second this points to arguments


the one upstairs is quite right. Here is a sentence to tell the landlord to judge that this points to the problem: whoever calls it, this points to him. There is no caller. In non-strict mode, it is window, or undefined


.

I feel fn () you should be able to call the function directly.

arguments [0] () you can take a look at this.

var obj = {
    fn: function () {console.log(this)}
    1: function () {console.log(this)}
}
obj.fn()
obj[1]()
// arguments[0]()
< hr >

is there a problem?


the first one points to window, the second one points to method


the first fn () this points to the length property of the win win object and there is no defined value, so the second this that returns undefined, points to the argumengts object, because the fn is stored as a parameter in the arg object, and the length of argumengts is 1


.

the correct answer is:

undefined
1

the first thing to understand is that function points to window objects before binding this .
second, you must understand the parameter arguments . Refers to all arguments of function , independent of formal parameters. That is, when the function is called, the parameters actually passed in exist in the form of an object: {"0": "," 1 ":"} , and the value in this question is: {'0values: [Function: fn]} . Then arguments [0] is [Function: fn] . When arguments [0] () is called, this points to the arguments object, which is equivalent to arguments.length , so the value is 1

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b32a21-2be11.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b32a21-2be11.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?