js this point to problems

1、fun()  

  this is the point of the window

            There obj = {
                a:3,
                fun:function(){
                    var a=5;
                    return function(){
                        alert(this.a);
                    }
                }
            }
            obj.fun()();

 

 

2、obj.fun() 

  this points to the obj

            was Obj2 = {
                a:3,
                fun:(function(){
                    var a=5;
                    return function(){
                        alert(this.a);
                    }
                })()
            }
            obj2.fun();

 

 

3, Timer

setTimeout(function(){
    console.log(this);
},1000);

 

 

4 、new

With the new function calls, the context is a function of blank objects created within secret

Call the function with a new will to go through four steps:

(1) create an empty object secret

(2) this will bind to the empty object

(3) the statement is executed

(4) return the object

 

 

 5, of course, it is the last call and apply and bind not to say

 

 6, there is an event handler, this points to the DOM element

 

Limited knowledge, I do not know there is not no summary, if any, will inform you about the big brother little brother

Guess you like

Origin www.cnblogs.com/lilei-site/p/11626694.html