Another question about this

I am looking at the Volume of javascript you do not know. On page 80, the lexical scope of using this to implicitly reference functions error code is as follows:

function foo(){
  var a = 2;
  this.bar();    //1
}
function bar(){
  console.log(this.a);
}
foo();

after deleting the this at code comment 1 on VS Code (as mentioned in the book), I found that although there was no error in the code, the result was undefined. When stepping into the call stack, it is foo- > bar, that is, this is bound to the foo function object as I thought in bar ().

after being puzzled, additional a variables with different values are defined in the global and bar functions, but the result is still undefined. It suddenly occurred to me whether it was due to the automatic use of strict mode, but testing on each browser console was also the result of undefined, and there was no solution. Help! Where on earth is this this bound?!

Mar.10,2021

if the call stack of this,bar before bar () in foo () is indeed foo- > bar, that is to say, the call location of bar is in foo. But you have a misconception that "the location of the call determines the binding of the this" does not mean that you can judge the binding of the this as long as you find the location of the call.
finding the call location is the first step in judging this binding. The second step is to use the rules of this binding at the call location to determine which binding this belongs to at this time, and then find the binding object of this. This is clearly explained at the bottom of page 83 of js you don't know.

in this code, we find the location of the call to bar, and then we find that it is a normal function call, not an object method reference and no binding function. So this applies the default binding (js you don't know, pages 83 and 84), that is, this binds to the global object, which does not have the an attribute. According to the lookup rules for lexical scope in non-strict mode, this returns a value of undefined.


ordinary function calls in browsers, whether this is the this browser of global, global environment in window,node or whether window,node is module.exports this or who depends on how the function is called


  1. in this example, it is impossible to access variables in foo scope in bar
  2. in the browser, the this in bar points to the global variable. Not
  3. in node

this is a global window object, and an is a local variable in your foo method. So it's undefined. You can take a look at this, the four scenarios of js this .


there is nothing wrong with undefined. After reading it, don't ask me that this, strongly recommends: https://codeshelper.com/a/11.


thsi.an in

bar does not exist.


the scope of this is related to the environment in which it runs. It points to the object that calls a method or object. For example, here, if it is node.js, it will always point to global;. If it is a browser, it will always point to window. However, in the node.js environment, variables declared at the outermost layer in the form of var a = 1 are not bound to the global of node.js, but are declared in a similar manner as a = 1 .

Menu