tags:

views:

35

answers:

2
function() { var w = new Object(), w.x = 10, y = 11; }
SyntaxError: Expected ';'

whereas,

function() { var w = new Object(), x = w, y = 11; }

produces no error and x does have w. What is wrong with the first one?

+2  A: 

You can not declare a variable called w.x.

Iamamac
Oops, yeah stupid question, what was I thinking! thanks.
Murali
+1  A: 

The var keyword is used to declare variables.

The only things you can put in the comma-separated list following the var keyword are variable declarations.

Therefore, your first syntax is invalid - it's read as declaring three variables (w, w.x, and y).
The assignment to w.x is a statement (which assigns a value to the w property of x), not a variable declaration, so you need a semicolon to terminate the var statement.

SLaks
Thanks SLaks, I was trying to declare a variable inside w which is not correct.
Murali