Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

why do i need indirection in jquery/javascript?

 
author & internet detective
Posts: 40748
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Jeanne Boyarsky
author & internet detective
Posts: 40748
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
author & internet detective
Posts: 40748
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Working code. Comments on any anti-practices in this are welcome.


 
Sheriff
Posts: 67592
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a document ready handler inside a function?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
author & internet detective
Posts: 40748
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67592
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
author & internet detective
Posts: 40748
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic