File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it better practice to pass an element object or element id to a function

 
C Brown
Greenhorn
Posts: 16
Android Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I pass an element object to a function or just the element id? I ask because I am having trouble getting the following to work (I'm trying to write a function that will either fade or display a div)...


The original function call is


When I run this code I get this error, "missing ] after element list" (in my example its line 5). When I pass in an element object instead of an element name I get this error, "el.style is undefined". Obviously I'm doing something wrong since neither one of them work, but if you could tell me which way is the "better" way or if it really doesn't matter. Thanks in advance.
 
C Brown
Greenhorn
Posts: 16
Android Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to get my function to work by fixing the setTimout function.

But if somebody could still tell me the better practice it would be much appreciated.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look how this string is built if el = "foo";



renders as:



so that means you are missing quotes around your string.

Better solution is to use a closure



Reason why a closure is better is you do not have to worry about quotes. The main reason you get better performance. When you use a string, the string needs to be evalutated. When you pass in a function reference, there is no evaluation step.

Eric
 
C Brown
Greenhorn
Posts: 16
Android Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never heard of closures before so I had to google it, they seem a little confusing. I've found some tutorials so I'll give them a shot and see how it goes. Thanks for the tip.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Closures are not that hard, people make them seem harder than they are. If you want to do advanced JavaScript coding, you need to know closures.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Closures and function contexts are essential concepts to writing good script, yet most page authors have no idea how they work.

They are such an important concept that I dedicated an entire appendix to explaining them in jQuery in Action. If you know someone with a copy of the book, it's worth reading.
 
C Brown
Greenhorn
Posts: 16
Android Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll check it out.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic