aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes why do i need indirection in jquery/javascript? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "why do i need indirection in jquery/javascript?" Watch "why do i need indirection in jquery/javascript?" New topic
Author

why do i need indirection in jquery/javascript?

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29246
    
139

I have a JavaScript widget as shown below that I'm converting to register with jQuery's event handlers (so I can call more than one event at a time.) It works fine when I assign my function to a local variable. When I refer to "this.myFunction" directly in the jQuery ready construct, I get "handler is undefined". Why is that? I'm not clear on why I need the extra level of indirection.



[edited to correct error message]


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29246
    
139

Not only have a done this correctly before, but I had a comment in the code because I knew I would forget. See callbacks here
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29246
    
139

Working code. Comments on any anti-practices in this are welcome.


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

You have a document ready handler inside a function?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The "this" issue is one of my very few *major* beefs with JavaScript-the-language. It makes sense when you think about it, but JavaScript makes you think about binding more than a lot of other languages. It's the cause of a *lot* of bugs and questions, that's for sure.

I think the List Apart article on JavaScript binding is pretty good.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29246
    
139

Bear Bibeault wrote:You have a document ready handler inside a function?

Um. Yes. Clearly that isn't a good idea from your reaction. Where should it be? Inside the .js file but outside the scope of MyWidget?

Before I started this, I had something like the following inside the function:
form[id].onFocus = handleFocus.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

Yes, it's unusual. Do you call MyWidget in top-level code?

Usually the document ready handlers (there can be as many as you like) are declared at top-level so that they register the ready handlers as the page is being evaluated prior to display.

Another jQuery best practice: unless it's necessary because a function is to be called from more than one location, the creation of named functions is regarded as needless. You'll generally see one-time functions declared inline as closures.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29246
    
139

Bear,
I see. Now that you mention it, the ready part is redundant. MyWidget is instantiated in the initialization method (called from onload) in the main HTML. The fact that the code gets run implies the page has loaded. [And yes, I know this isn't a jQuery practice - the JSP I'm working on predates us using jQuery.]

Also good point about the unnecessary named function. it was there before I started changing things so I didn't think anything of it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why do i need indirection in jquery/javascript?
 
Similar Threads
A couple questions regarding Jquery
drag n drop & file api
What does this do? (function($){ ... })(jQuery);
How to call jquery method in Struts.
console.log function not working.