最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dojo.hitch() scope question - Stack Overflow

programmeradmin2浏览0评论

Why is it that when I use the dojo.hitch function and try to reference the "this" operator inside it, that it gives me reference to the wrong object?

console.debug(dijit.byId("widgetName"));  //works as expected and prints out the window object

dojo.hitch(dijit.byId("widgetName"), tester())();   //this should be executing the tester function in the scope of the widget object

function tester() {
    console.debug(this);    //prints out the Javascript Window object instead of the widget object!!!!
}

Thanks

Why is it that when I use the dojo.hitch function and try to reference the "this" operator inside it, that it gives me reference to the wrong object?

console.debug(dijit.byId("widgetName"));  //works as expected and prints out the window object

dojo.hitch(dijit.byId("widgetName"), tester())();   //this should be executing the tester function in the scope of the widget object

function tester() {
    console.debug(this);    //prints out the Javascript Window object instead of the widget object!!!!
}

Thanks

Share Improve this question edited Aug 31, 2011 at 14:53 Ayyoudy asked Aug 31, 2011 at 14:16 AyyoudyAyyoudy 3,72112 gold badges49 silver badges66 bronze badges 5
  • That should not happen, don't believe that what you have posted is the same as what you are attempting in your application. Any scnreenshots to back it up? – Layke Commented Aug 31, 2011 at 14:21
  • 1 Are you sure it returns correctly if you put console.log(dijit.byId("widgetName")) just before the hitch? Does it return null by any chance? In that case, this refers to window. – pimvdb Commented Aug 31, 2011 at 14:22
  • @Layke I know it shouldn't happen, hence my question. Been scratching my head over it for sometime. This is a copy/paste code exactly from my app. I don't know what you want to see in the screenshot?! – Ayyoudy Commented Aug 31, 2011 at 14:23
  • @pimvdb, I tried that and it prints out the widget just fine. I really don't understand this and it is driving me insane... – Ayyoudy Commented Aug 31, 2011 at 14:29
  • @Layke I modified the question slightly because I changed my code since so I can debug it easier but it's basically the same result. Any ideas what i can try next? – Ayyoudy Commented Aug 31, 2011 at 14:55
Add a ment  | 

1 Answer 1

Reset to default 6

Based on what you have just shown, I can now safely provide an answer explaining what is wrong.

When you do a dojo.hitch() you should not call the function inside of it, but instead call the result of the function. That is, you need to provide dojo.hitch with a reference to the function to hitch, not the result of invoking that function.

In your example, you are calling tester() (which invokes the function tester) inside of the dojo.hitch(), which calls the tester once. Even though you have dojo.hitch()(); because tester() does not return a function handler (but the result of tester, in this case undefined), the hitch()(); does nothing. That might have been confusing so I will show you with an example.

Don't do this:

dojo.hitch( context, handler() )();

Instead do this:

dojo.hitch( context, handler )();

So to make what you have very readable you would do this:

widget = dijit.byId("widgetName"); 
tester = function() {
    console.log(this);
}

handle = dojo.hitch(widget, tester);
handle();

Your mistake was trying to call the function from within the dojo.hitch(). This mistake also was not present in your original question.

发布评论

评论列表(0)

  1. 暂无评论