JSFiddle - React, Tailwind, and code Playground

by Deepak Anand

HTML

<button id ="btn"> Without binding </button>
<button id ="btn2"> Bind using lang.hitch </button>
<button id ="btn3"> Bind using native bind </button>

JavaScript

// Usage of lang.hitch and Function.prototype.bind

require([
    "dojo/_base/declare",
    "dojo/_base/lang",
    "dojo/on",
    "dojo/dom",
    "dojo/domReady!"
    ], function(declare,lang, on, dom){           
   
    var MyObject = {
        
        myProperty: "foo",
        
        handleClick: function(e){           
            console.log(this.myProperty);                        
        }        
    };
    
	// context is lost
	on(dom.byId("btn"), "click", MyObject.handleClick);
   
   
    on(dom.byId("btn2"), "click", lang.hitch(MyObject, MyObject.handleClick));
    
    
    on(dom.byId("btn3"), "click", MyObject.handleClick.bind(MyObject));
    
     

        
});