How to understand the point of this question in JS

First of all, explain this sentence is: pointing object performs the current function.

Currently executing, understand it, that this point in time is a function definition can not be determined only when the function is performed can be determined. Who in the end this point? The final point of this is that it calls the object (although in most cases this does not understand the problem, but in fact is not accurate).

1. Default execution

Example 1:

        Fn function () { 
            var = User "ADMIN"; 
            the console.log (this.user); // undefined 
            the console.log (the this); (non-strict mode) // window 
        } 
        Fn ();

As we said above, this is the final point of calling it an object, where the function was actually a point out of the Window object, the following code can be proved.

        Fn function () { 
            var = User "ADMIN"; 
            the console.log (this.user); // undefined () 
            the console.log (the this); (non-strict mode) // window 
        } 
        window.fn ();

2. Object execution:

Example 2:

var obj = {
            user:"admin",
            fn:function(){
                console.log(this.user);  //admin
            }
        }
        obj.fn();

this is the point where the object obj, because you call this fn by obj.fn () execution, that nature is pointing to the object obj, we emphasize that here again, this point when the function is created not decide, in when called to decide, who called on the point who, we must understand this.

3. overthrow the theory:

In fact, Example 1 and Example 2 say is not accurate enough, the following example can overthrow the theory above
example 3:

        var obj = {
            user:"admin",
            fn:function(){
                console.log(this.user); //admin
            }
        }
        window.obj.fn();

This code is part of the code above is almost the same, but this is not the point here is why the window, if in accordance with the above theory, the final point of this is to call its object

Not here to explain why the above code during this why there is no point window, we look at a piece of code.

        var obj = {
            a:10,
            b:{
                a:12,
                fn:function(){
                    console.log(this.a); //12
                }
            }
        }
        obj.b.fn();

Here also the object obj points out, but again this did not execute it, then you would certainly say that I started to say that it is wrong not to do? In fact, not only said at the beginning is not accurate, then I will do some additional, I believe you can completely understand the problem points of this.

  Case 1: If there is this a function, but it has not been invoked on an object, then this point is the window, there should be noted that in the strict version of this js not point window, but not here Discussion strict version of the problem, you want to know can go online to look for.
  Case 2: If there is this a function, this function has the object to be called on stage, so this point is on an object.
  Case 3: If this function, this function contains a plurality of objects, although this function is called outermost objects, this point on it is only an object.

There is also a special case:

 Example 4:

 
 var obj = {
     a:10,
     b:{
         a:12,
         fn:function(){
             console.log(this.a); //undefined
             console.log(this); //window
         }
     }
 }
 var j = obj.b.fn;
 j();

  Here this points to the window, because this always points to the last of its object calls, also is to see when it is executed who invoked the example 4, although the function fn is the referenced object b, but at the fn assigned to when the variable j is not executed so the final points to the window, and this is not the same as example 3, example 3 is a direct implementation of fn.

this is actually going to speak in terms of that one thing, just in different environmental conditions will be different, so I have no way to explain once and can only slowly go experience.

Guess you like

Origin www.cnblogs.com/xushipobk/p/11443148.html