What is the difference between:
ANDCode:function removeLightbox() {...}
Code:removeLightbox = function(){...}
| SitePoint Sponsor |
What is the difference between:
ANDCode:function removeLightbox() {...}
Code:removeLightbox = function(){...}



Second one is more flexible, allowing you to declare function depending on conditions, like:
then second one can be used inside objects, like $.removeLightbox = function() {}; which can't be done with first method.Code:if(whatever) { var removeLightbox = function() { return 'f1'; }; } else { var removeLightbox = function() { return 'f2'; }; }
With second method you can't use function before its declaration, but with first method you can.
That's all I can think of. I'm sure there are many other differences.
ColorizeIt - online color editor for forum styles.
Thanks, that helps for now


The first one declares a function using the function statement. The second declares a function using a function operator.
The main difference between them is that functions created using the function operator do not exist until execution reaches that code. Function statements are hoisted to the top of their context. That means that a function statement can be made at the bottom of the code, but it can still be used from places above the function statement.
So while the function statement has hoisting as its advantage, it's a hidden advantage that most people aren't explicitly aware of, even though they implicitly make use of it.
The function operator allows you to create functions just when they're needed.
Programming Group Advisor
Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
Car is to Carpet as Java is to JavaScript
Bookmarks