|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
TOP THREE LINKS YOU MUST CLICK ON AJAXWorld News Desk AJAXWorld RIA Conference - Speaker Mike Girouard's JavaScript Design Patterns, #1
Self-invocation - one of the most useful and commonplace patterns
By: Michael Girouard
Aug. 25, 2008 07:52 AM
Mike Girouard's Blog MotivationThe primary motivation behind self-invoking functions is to create scope. In JavaScript, only functions have scope. Any time variables are defined outside of a function, they are carelessly dumped into the global object. ImplementationA self-invoking function is a standard function, just with a set of parentheses appended to the end. function () { The above example defines an anonymous function. This creates a literal function, yet since no name is attributed to it, the value is never stored. The trailing parenthesis tell the function to execute, just as if you were calling a named function. Upon execution, the above function creates three variables, formats them, and alerts them to the user. Once the function terminates, the variables are discarded and the global object remains unchanged. Distinguishing from Actual FunctionsGiven the oddness of the pattern (and lack of widespread understanding), it is very possible for developers to misinterpret this pattern as an actual function. It it recommended that an extra set of parentheses wrap the function definition as well so to provide a visual clue to the developer that the function isn’t a normal function. The result would be as follows. (function () { Passing ParametersIn the event where a self-invoking function requires parameters, they can be passed in the same manor that you would a regular function. The following example applies an “negative” class on every input element who’s numeric value is below 0. (function (elements) { Executing in Another ScopeEven though the function is executing within its own local scope, the this keyword will still refer to the global object. The following example uses the Function.call() method to execute a self-invoking function within the scope of the first table element on the page. (function (elements) { ConclusionIn an effort to protect the global object, all JavaScript applications should be written within a self-invoking function. This will create an application scope in which variables can be created without the fear of them colliding with other applications. If a global variable is needed, developers must take the extra step by setting them directly within the window object. For example window.foo = ‘bar’;. Self-invocation is a fundamental pattern of professional JavaScript development. Nearly every pattern in this weeks catalog uses it as a base to create scope, closure, or to configure cross-platform objects on-the-fly. LATEST AJAXWORLD RIA STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||