What's the use of lang.hitch in ArcGIS API for java?
Can someone explain lang.hitch() function used in ArcGIS API for JavaScript?
require(["dojo/_base/lang"], function(lang){
var myObj = { //A simple object with the property of foo
foo: "bar"
};
var func = lang.hitch(myObj, function(){ //Using lang.hitch we can point to the scope that we want. Now "this" is pointing to myObj
console.log(this.foo);//Grabbing the contents of food that is in a different scope
});
func(); //Call the function printing out this.foo
});
In the code above we have a simple myObj object with a property of foo. That object is in a different scope than the function below it. When we call our function called func, we add lang.hitch and specify the scope which is myObj. This allows us to grab the contents of foo even though it is in a different scope by calling this.foo.
If we did not have the lang.hitch we would get an error saying the foo was undefined.
For more info dojo/_base/lang