Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 20th, 2011, 12:51 PM
jwg jwg is offline
Registered User
 
Join Date: Jul 2008
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?
 
Old December 21st, 2011, 03:14 AM
jmcpeak's Avatar
Wrox Author
 
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 18 Times in 17 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 03:15 AM.. Reason: typo
 
Old December 21st, 2011, 10:53 AM
jwg jwg is offline
Registered User
 
Join Date: Jul 2008
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.





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 08:46 PM
Executing a function on every page.. kingroon ASP.NET 2.0 Basics 5 January 14th, 2008 11:31 AM
Executing a function mepancha SQL Server 2000 0 April 28th, 2005 10:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.