This question has already been answered:

var obj = { name: 'cc', trade: function() { console.log(this.name); //cc } }; obj.trade(); var obj = { name: 'cc', trade: function() { console.log(this); //name, trade (function() { console.log(this); //window, в этом и вопрос, почему, //ведь эта функция выполняется внутри метода объекта })(); } }; obj.trade(); 

Why does this function, which is inside the trade method of obj , equal to window , and not obj ?

Reported as a duplicate by the members of ThisMan , Grundy javascript Nov 7 '18 at 6:04 pm

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    Code - text! (Welcome to StackOverflow!) - Igor

1 answer 1

Because what matters is not where the function is located, but how it is called:

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