Object

by sikusikucom

JavaScript

var jim = {
    name: "Jim",
    "favorite color": "green",
    greet: function() {
        console.log("Hello, my name is " + this.name);
    }
};

var mary = {
    name: "Mary",
    greet: jim.greet // without () because we're not calling the method
};

mary.greet(); // prints "Hello, my name is Mary"

var myFunction = jim.greet;
// When called by myFunction(), 'this' refers to the 'window' object.
myFunction(); // prints "Hello, my name is"

function whatIsThis() {
    console.log(this);
}
whatIsThis();  // 'this' inside the function refers to 'window'