This keyword problem

as shown in the figure, why does this point to windows/undefined, after calling obj.getAge ()? isn"t obj calling it? shouldn"t this point to obj?
clipboard.png

:personshowNamethispersonthis
clipboard.png

Feb.26,2021

because fn is an anonymous function

give a simple example

    var obj = {
        fn1: function() {
            console.log(this);//obj
            var noNamed = function() {
                console.log(this);//windowd
            };
            return noNamed();
        }
    };
    obj.fn1();

above, your var fn=function () {}; then return fn () is essentially a directly called anonymous function closure return function () {}
you just assign this anonymous function to fn, and it doesn't change the nature that he doesn't have a function name

.

conclusion: as long as it is an anonymous function, this points to the window global object, for example:

function foo() {
    var fn = setTimeout(function() { //
        console.log(`setTimeoutthisfoo:${this.age}`); 
    }, 0);
}
foo.call({
    age: '1000'
});//undefined

var name = 'XL';
var person = {

name: 'xl',
showName: function() {console.info(this.name)}

}

person.showName ();
/ / where the showName call location is the person context, so the active this is bound to the person
var showNameA = person.showName;
/ / the showNameA here is similar to the function alias, showNameA is a reference to person.showName, and the showNameA call location is still window.
/ / the binding of this is related to its call location

.

var obj = {

birth: 1990,
getAge: function() {
    var b = this.birth;
    var fn = function() { console.info(this)}
}
return fn;

}

the getAge in your obj involves closures, which can actually be understood as returning a function fn,obj.getAge () to continue to execute fn, after completion. The location of the call at this time is in the Window environment, so the this in fn points to window

.

this doesn't understand

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