Link Details

Link 91831 thumbnail
User 111696 avatar

By bloid
via blog.quinthar.com
Published: Jul 02 2008 / 18:03

In any reasonable language, it would first alert "Hello", and then alert "World". But noooo. Javascript's gotta get all fancy and defer execution until later. And at that point message is equal to "World" for both. So it's "World" "World", which ain't right at all.
  • 7
  • 13
  • 1245
  • 600

Comments

Add your comment
User 204084 avatar

kenman replied ago:

0 votes Vote down Vote up Reply

Was good for a laugh.

User 201716 avatar

unchqua replied ago:

0 votes Vote down Vote up Reply

But this can be useful if you know this feature and use it wisely.

User 261835 avatar

Kenneth Downs replied ago:

0 votes Vote down Vote up Reply

I voted up because the article opens the discussion of the behavior of anonymous functions, which can be counter-intuitive if you are used to other languages.

User 209464 avatar

willcode4beer replied ago:

0 votes Vote down Vote up Reply

JavaScript didn't defer execution, the author did by putting the calls to the functions at the end.

He's apparently confused that the global variable 'message' is.... global.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.