lang.hitch what does it do?

20499
3
Jump to solution
02-20-2015 08:12 AM
TimWitt2
MVP Alum

Hey everybody,

Looking at some of the widgets code I keep coming across lang.hitch. What does this exactly do? What is it used for?

Any example that even I can understand?

Thanks,

Tim

1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Tim,

   lang.hitch will really become your friend in WAB widget development. Here is my definition:

lang.hitch enables you to keep a code block in scope. Say you have a function that has a inline function and you want to call this.map to get a refernec to the map object well if you are calling this.map from inside an inline function you are VERY like to see the error that this.map is undefined. Using lang.hitch before you call your inline function tell the inline function what 'this' is.

dojo/_base/lang — The Dojo Toolkit - Reference Guide

hitch() returns a function that will execute a given function in a given context. This function allows you to control how a function executes, particularly in asynchronous operations

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

Tim,

   lang.hitch will really become your friend in WAB widget development. Here is my definition:

lang.hitch enables you to keep a code block in scope. Say you have a function that has a inline function and you want to call this.map to get a refernec to the map object well if you are calling this.map from inside an inline function you are VERY like to see the error that this.map is undefined. Using lang.hitch before you call your inline function tell the inline function what 'this' is.

dojo/_base/lang — The Dojo Toolkit - Reference Guide

hitch() returns a function that will execute a given function in a given context. This function allows you to control how a function executes, particularly in asynchronous operations

TimWitt2
MVP Alum

Robert,

thanks for the explanation now I know why in some of my functions this.map was undefined. I solved it by using var myMap = this.map in the function and referred to it in my inline function as myMap.

But the way you explained it makes much more sense.

Tim

LefterisKoumis
Occasional Contributor III

I totally agree. At first, I had no idea what Robert meant since my knowledge on JavaScript was limited.

For those of you who are reading this, and don't understand yet the implications of using the keyword "this", take a look at this very useful reference:

Understand JavaScript’s “this” With Clarity, and Master It | JavaScript is Sexy