This
Regular Mode
  • this refer to the owner object
  • // in a method
    var person = {firstName: "John", lastName : "Doe",
      		fullName : function() { 
        			return this.firstName + " " + this.lastName; // refer to person
      			}
      		};
    
    console.log(person.fullName());
    
    // alone
    var x = this; // refer to window
    
    console.log(x);
    
    // in a function
    function f()
    {
      	return this; // refer to window
    }
    
    console.log(f());
    		
    Strict Mode
  • this in a function is undefined
  • "use strict";
    
    // in a function
    function f()
    {
      	return this; // undefined
    }
    
      		console.log(f());
    		
    This in Event Handlers
  • Refers to the HTML element that received the event
  • <button onclick="this.style.color='red'">Click</button>