this, scope and binding

by LyndseyB

HTML

<input type="button" id="myButton" value=" Click Me " />
<div id="myDiv"></div>

JavaScript

function MyFunction() {
    this.message = "Hello Lyndsey";
    this.showMessage = function() {
        //console.log(this);
        $('#myDiv').text(this.message);
    };  
   
    $('#myButton').on('click', this.showMessage);  // nothing, because message is not a property of the button object
    //$('#myButton').on('click', this.showMessage.bind(this)); // Lyndsey 
    
}

new MyFunction();