View Full Version : "Pop quiz, hot shot"

Alex Vincent
06-07-2012, 07:59 PM
(a la Dennis Hopper in "Speed")

var X = {}, result = "PASS"
X.Exn = function() { result = "FAIL"; }
(function() {})();

Without running this code in your browser or other JS engine, what do you think this code will do?

Hint: I wrote this code, and I got it wrong.

06-07-2012, 08:49 PM
Um...will it "shoot the hostage?" :D

06-08-2012, 11:57 AM
It will do nothing?

06-08-2012, 12:06 PM
It will do nothing?

Nothing that anyone will see anyway - without more code to actually access the values set.

It will end up with X defined as an object with an Exn method attached.
It will end up with a variable called result that contains the value 'PASS' and where if the Exn method were ever to be run that value would be changed to 'FAIL'.

It is the last line that does absolutely nothing at all since it is a self executing anonymous function with nothing in it to actually run.

Of course the Exn method isn't run by the code so the value of result doesn't get changed and since there's no code to actually display the values the only way you'd tell if the value were to be changed would be to examine it in the built in debugger that all browsers except Firefox now have (you have to install either the Venkmann or Firebug extension to add a debugger to Firefox equivalent to the ones built into Internet Explorer, Chrome, Safari, and Opera).

Alex Vincent
06-08-2012, 10:42 PM
That's the same logic I made. It's also wrong, because X.Exn does run. Perhaps if I make one little change:

var X = {}, result = "PASS"
X.Exn = function() { result = "FAIL"; } (function() {})();

As an expert explained to me, it's saying "execute that first function with the second function as the first argument of that first function. Then execute whatever that returns."

This has to do with automatic semicolon insertion: there was no semicolon at the end of the second line. So, it tried to execute that function. But ultimately it threw the exception.

Semicolons are transparently inserted in a lot of cases. This is one where it's not, because we're dealing with the end of a function body and the beginning of an argument list passed into the function.

My rule of thumb has been "if you have a closing brace, you don't need a semicolon." I may need to change that rule...

06-09-2012, 02:12 AM
That's the same logic I made. It's also wrong, because X.Exn does run.

Ah, I overlooked the missing semi-colon on the end of the second line.

You always need a semi-colon after a } that ends an anonymous JavaScript function - its only when you define named functions that you don't need it (and of course named functions can only be used in very limited circumstances in JavaScript so its easier to always just assign anonymous functions to variables instead since that always works - as long as you don't leave off the semi-colon).

The only spot where adding a semi-colon after a } would break the code is where the } is followed by else or while so a better rule would be to always insert the semi-colon except if it is immediately followed by one of those two words.

06-10-2012, 02:47 AM
Good observations.
Unintended consequences can drive you crazy.

<div id="info"></div>

var Sheldon = function(n){



If you wrap an assignment in parenthesis after a function - it becomes plain the statement is actually an argument for the preceding function.

var f = function(n){

(a = "hello world" , b=["a second argument",""]);