There are some questions about the this direction in setTimeout ().

 var x = 1;
    var o = {
        x: 2,
        y: function() {
            console.log(this.x);
        }
    };
    setTimeout(o.y, 1000); 
    
:1,thiswindow
 var x = 1;
    var o = {
        x: 2,
        y: function() {
            console.log(this.x);
        }
    };
    setTimeout(o.y(), 1000); 

the result of execution is: 2

this is pointed to o

.

Why does adding () change the direction of this?

Aug.27,2021

indicates the call in parentheses, without parentheses indicates the function body, the first call is window, so the result is 1 and this pointing to window, and the second time, as you typed, the object o is called after parentheses, so of course it points to the object itself, so it is 2. / p >


the previous one is equivalent to

setTimeout(function() {
    console.log(this.x);
}, 1000);

setTimeout internal this points to window

the latter is equivalent to

o.y()
setTimeout(undefined, 1000); 

o

the yjournal this executed points to o

.

o.y passes a function, which is the syntax of window.setTimeout (fn,times) , indicating that fn is executed after times milliseconds, obviously by window

.

o.y () means to execute the function immediately, which has nothing to do with the timer. At this time, the executor is o

.

o.y is function . Execute
o.y () immediately after 1s. The result is 2


.

1 is a direct call, pointing to the global;
2 is a method call, pointing to the caller of the method.

For more information, please see link

.
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-1bf559a-31f88.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-1bf559a-31f88.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?