Jump to content

Anonymous=Function(){[Codes]}


Recommended Posts

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.})();

  • Like 1
Link to post
Share on other sites
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 post
Share on other sites
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? Edited by O. Samuel
Link to post
Share on other sites
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. :umnik2: Weird. Anyway, I guess I'm wrong. ^_^ Not sure what caused the error in FireBug. Maybe a bug in FireBug.

  • Like 1
Link to post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...