4

I recently came across this variable initialization in a WebGL tutorial:

var mouse = { x: 0, y: 0 }, INTERSECTED;

I've never seen this format. I understand it's creating an object with an x and y property, but how is INTERSECTED related to the variable/object?

Thanks!

7
  • 3
    I believe that INTERSECTED is just being initialized. like var INTERSECTED; Commented Dec 5, 2012 at 0:19
  • More info here en.wikipedia.org/wiki/Comma_operator Commented Dec 5, 2012 at 0:20
  • @elclanrs: It's not actually the comma operator. It's just that the var statement uses the same character as a separator. Commented Dec 5, 2012 at 0:24
  • Exactly. That's what the wiki says, it provides the example of separator as well as operator. Commented Dec 5, 2012 at 0:25
  • Thanks all! The answers always simpler than my brain wants to let it be. Commented Dec 5, 2012 at 0:36

4 Answers 4

9

The line is simply declaring two variables (mouse and INTERSECTED), and initializing mouse to { x: 0, y: 0}.

INTERSECTED is not necessarily related to mouse, though clear code should only declare multiple variables together if they are highly related (even then, many prefer to declare every variable on a separate line).

Sign up to request clarification or add additional context in comments.

1 Comment

They are highly related within the script, I think that is what threw me off. Thanks for your answer!
3

I may get it wrong, but I think It is just like

var A=3,B;

Defines A with value 3 and B uninitialized. INTERSECTED is just another var.

Comments

3

INTERSECTED isn't related to mouse. It's just a one line way of initializing two variables. I'm guessing that INTERSECTED is there so that it is treated as a local variable.

Comments

0

Just adding my 2 cents: the usual convention is to write them on two separate lines and this certainly avoids any confusion:

var mouse = { x: 0, y: 0 },
    INTERSECTED;

(also note that the convention is to have a four space indent which nicely aligns the variables)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.