Drycodez Posted October 4, 2011 Share Posted October 4, 2011 Is it advisable to be using anonymous function instead of the normal regular function all-the-time?REGULAR function dName(){[code]} ANONYMOUS dName=function(){[code]} Link to comment Share on other sites More sharing options...
Ingolme Posted October 4, 2011 Share Posted October 4, 2011 If you declare a function normally, it can be executed anywhere in the script. If it's an anonymous function it can only be executed after it has been declared. alert(x()); // Prints 1function x() { return 1;} alert(x()); // This won't work, and the error console will say "x" is undefinedvar x = function() { return 1; }; Use anonymous functions for closures or when something is dynamic.A closure: (function() { var document = "Some text"; // The closure protects the window.document object from being overwritten.})(); Link to comment Share on other sites More sharing options...
ShadowMage Posted October 4, 2011 Share Posted October 4, 2011 Anonymous functions are also useful when assigning event handlers: document.getElementById('anElement').onclick = function() { //...do something} Link to comment Share on other sites More sharing options...
ShadowMage Posted October 4, 2011 Share Posted October 4, 2011 If you declare a function normally, it can be executed anywhere in the script. If it's an anonymous function it can only be executed after it has been declared.alert(x()); // Prints 1function x() { return 1;} alert(x()); // This won't work, and the error console will say "x" is undefinedvar x = function() { return 1; }; Actually, this is incorrect. Neither of those codes will work. JavaScript is not a compiled language, so it is executed line by line as it is read. So when it tries to run alert(x()); the function x has not yet been read so it does not exist. Link to comment Share on other sites More sharing options...
Drycodez Posted October 4, 2011 Author Share Posted October 4, 2011 Actually, this is incorrect. Neither of those codes will work. JavaScript is not a compiled language, so it is executed line by line as it is read. So when it tries to run alert(x()); the function x has not yet been read so it does not exist. Well, i think ingolme is wright! Why not give it a try and c 4 yourself? Link to comment Share on other sites More sharing options...
ShadowMage Posted October 4, 2011 Share Posted October 4, 2011 Well, i think ingolme is wright! Why not give it a try and c 4 yourself?Hmm....that's odd. I tried it in the FireBug console with this code:console.log(test());function test() { return "This is a test";} ...and got this error: ReferenceError: test is not defined After your post I tried it again, got the same error, and then moved the console.log() line after the function and it ran just fine. Then I moved it back to before the function to verify that it doesn't work and it still ran with no error. So then I created a blank HTML document with the same code (but a different function name) and tried and it worked without any errors. Weird. Anyway, I guess I'm wrong. Not sure what caused the error in FireBug. Maybe a bug in FireBug. Link to comment Share on other sites More sharing options...
Drycodez Posted October 4, 2011 Author Share Posted October 4, 2011 Hmm....that's odd...anyway, i guess i am wrong. you were wrong, but now, you are wright! Link to comment Share on other sites More sharing options...
Ingolme Posted October 4, 2011 Share Posted October 4, 2011 Javascript interpretters do several passes over the code before executing it. You could say it's "compiled" at runtime. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 4, 2011 Share Posted October 4, 2011 The Firebug console isn't necessarily the same as the browser executing the code on the page. It worked after you defined it once because the function was always defined after that in Firebug. Like Ingolme said, the Javascript interpreter is not a single-pass interpreter. The first time through the code it defines everything that is global, including functions. It only executes the code in a later pass. I'm not sure how Firebug was implemented, but that first pass doesn't happen when you run code in the console. Also, different runs through the console are not separate. If you paste code into the console to define a function, then clear the console and paste new code in that uses the function but without the function definition, it will still work because the function was already defined in whatever namespace the console uses. So working with the console is like running code on the page, it will only reset if you refresh the page. Also, browsers are actually beginning to compile Javascript the way we normally think of compilation. I've heard that IE9 can use multiple cores to compile the Javascript code in one thread and execute it in another. I believe the V8 engine in Chrome also compiles the code. The term is just-in-time (JIT) compilation when it compiles the code once just before executing it for the first time. Link to comment Share on other sites More sharing options...
ShadowMage Posted October 4, 2011 Share Posted October 4, 2011 JSG to the rescue once again! I was beginning to come to the same conclusions you outlined so nicely in the first paragraph, but I'd never have been able to put them to words... Link to comment Share on other sites More sharing options...
Synook Posted October 5, 2011 Share Posted October 5, 2011 A comment on the original question: if you name an anonymous function, then it is named and just as "regular" as your "regular" function — there is no technical difference between them (as functions are first-class objects in JavaScript). If you put the var keyword before the line of code in the second code box the scope will be limited, though. The latter syntax can also be used where the former can't in some cases, such as when assigning to member variables.In fact, you could think about the former statement as just being syntactic sugar, with the latter being the "real" code. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.