Explain why this points to window?

can you help me explain why this points to window

function fn(){
    console.log(this)
};

principle

Apr.27,2021

  1. the function is not bound to any object in this context (execution environment), which means that this points to window;
  2. is easy to understand in terms of scope and call chain. The upper level of the function fn is the global, and this this points to the global.
  3. if it is executed in strict mode, and in strict mode the this points to undefined.

Brother, have you lost the step-by-step process? When we write it, we usually write it simply. Normally, for example, alert is also an attribute of window. It should be written as window.alert, just as this function is window.fn (), when it is called, but we generally write it as a direct call to fn (), that is to say, it is called by window, so this points to window, and remembers that whoever calls this points to ok


.

under the conditions you give, this does not necessarily point to window

. The direction of

this depends on how the fn (function) is called. To sum up, there are three ways to call a function in javascript .
declare a function first.

function fn(){
    console.log(this);
}

the first type of , most commonly, is called as a property of an object, where the this points to the object that called it.

var obj = {
    go: fn
}

obj.go(); // thisobj

fn(); // 

//  
window.fn(); // this  window

// nodejs 
global.fn() // this  global

the second , new keyword call, this points to the newly created object instance (the object's _ proto__ points to fn.prototype ), which is a little more complicated. You can take a look at reference document .

new fn();

the third , call and apply are called. this can be specified by yourself.

var obj2 = {
    name: 'segmentfault'
}
fn.call(obj2); // thisobj2

this simple call


this belongs to bom and dom content

one or two sentences are not clear

I suggest that you systematically learn the basics of js (BOM DOM ECMAScript)
https://pan.baidu.com/s/1hqwt0R6

.

this objects are bound at run time based on the environment in which the function executes: in the global environment, this equals window, and the function is called as a method of an object, this equals that object.

function fn(){
    alert(this)
};
fn();//window.fn(),thiswindow
var obj = {};
obj.fn2 = fn;
obj.fn2();//thisobj

how to determine the value of this

clipboard.png


this refers to the execution environment of the current reference function refers to the object


in js, except that this refers to the formatting context of the current function execution function when writing the arrow function, the rest is determined during function execution. The question asked by the landlord seems to be less called.
if this is what the landlord wants to ask:
function fn () {

console.log(this)

};
fn ()
then in fact the function environment in which the fn function is called is global, that is, the window environment.
is window.fn () when actually called. So this is pointing to the window object


1.fn ();

windowthiswindow
var age = 38;
var obj = {
    age: 18,
    getAge:function () {
        console.log(this.age);// 18
        function foo(){
            console.log(this.age);// 38
        }
        foo();//window
    }
}
obj.getAge(); //

2. As a method obj.fn (), this pattern points to the object calling the method

    var fn = function(){
       console.log(this.length);
    }
    var arr = [fn, 1, 2];
    arr[0](); // 3

3. In constructor mode, this points to the object coming out of new

    function Person(){
        console.log(this);
    }
    var p = new Person();

4. Context mode call () apply () bind ()

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