Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: JavaScript 24-Hour Trainer
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: JavaScript 24-Hour Trainer
This is the forum to discuss the Wrox book JavaScript 24-Hour Trainer Jeremy McPeak; ISBN: 978-0-470-64783-7
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: JavaScript 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 20th, 2011, 11:51 AM
jwg jwg is offline
Registered User
Points: 29, Level: 1
Points: 29, Level: 1 Points: 29, Level: 1 Points: 29, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2008
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default using a self executing function

I'm just finished the chapter on creating an event utility object. I was interested to learn about self executing functions but I don't see why one was used in the code in chapter 20. I teach a course to high school students and don't like teaching them stuff if I can't explain why it's being done. I rewrote this code without using a self executing function and it works fine so why do it here? There is absolutely no explanation in the book of why this is done. I looked up reasons for using a self executing function and the only reasons I could find were in order to encapsulate variables and not polute the global object. Are these the reasons for doing it in chapter 20 or are there other reasons having to do with functionality?
Reply With Quote
  #2 (permalink)  
Old December 21st, 2011, 02:14 AM
jmcpeak's Avatar
Wrox Author
Points: 361, Level: 6
Points: 361, Level: 6 Points: 361, Level: 6 Points: 361, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2005
Location: , Texas, USA.
Posts: 77
Thanks: 0
Thanked 12 Times in 12 Posts
Default

Howdy, jwg.

The benefit is explained either in an earlier lesson or on the DVD; I imagine the latter, as it can be better explained with that medium. But for a quick and dirty explanation, it was used for performance reasons. Event utility methods are typically written like this:

Code:
addEvent : function(obj, evt, fn) {
    if (typeof addEventListener === "function") {
        // use standards-based code
    } else {
        // use legacy IE code
    }
}
It works, but the JavaScript engine has to evaluate the condition (the addEventListener test) every time addEvent() executes. It's a very small performance hit, but a hit none-the-less.

By using a self-executing function, the engine only has to evaluate the condition once at load-time. Since it returns a browser-specific function for adding an event handler, the resulting addEvent() executes as fast as possible.

Last edited by jmcpeak; December 21st, 2011 at 02:15 AM. Reason: typo
Reply With Quote
  #3 (permalink)  
Old December 21st, 2011, 09:53 AM
jwg jwg is offline
Registered User
Points: 29, Level: 1
Points: 29, Level: 1 Points: 29, Level: 1 Points: 29, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2008
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, thanks for the reply. I'm not crazy about learning from video, but now that I know there is stuff there that is different from the book I'll give the videos a look.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a JavaScript Function from XSL IronStar XSLT 5 July 8th, 2010 07:49 PM
XSLT not executing java function(using SAXON) dved XSLT 2 January 15th, 2008 07:46 PM
Executing a function on every page.. kingroon ASP.NET 2.0 Basics 5 January 14th, 2008 10:31 AM
Executing a function mepancha SQL Server 2000 0 April 28th, 2005 10:50 AM



All times are GMT -4. The time now is 12:46 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.