As we all know, in a method invocation, this refers to the object to which the method belongs, such as the following code:
Obj.test (Console.log (this// returns True
However, in nested functions, the situation has changed a lot. This needs to be divided into two situations: strict mode and non-strict mode
// obj = {test: function () { var self = this ; Console.log ( this === obj); True f (); function F () {Console.log (
this
=== obj); False Console.log (self === obj); True Console.log ( this === window); True}; }};obj.test ();
//under strict mode"Use Strict"varobj ={test:function (){ varSelf = This; Console.log ( This===obj); True f (); functionf () {Console.log ( This===obj); False Console.log ( self===obj); True Console.log ( This===window); False Console.log ( This===undefined); True}; }};obj.test ();
Understanding of the This keyword for nested functions in JavaScript