zok
zok

Reputation: 7882

variable three.js

I don't understand exactly what's happening here, it seems that's a syntax issue. Just in case it might be helpful, I'm very new to Javascript, but I have experience with AS3.

On this code:

https://github.com/mrdoob/three.js/blob/master/examples/canvas_lines.html

Line 34 is:

camera, scene, renderer;

I assumed these were variable declarations on the global scope, and also that in line 42, those were variable declarations as well but on the scope of the init() function.

Line 42:

particles, particle;

Playing around a little with that code I realized that the script didn't work if I declared the particle object on the global scope like that, but it works if i use it like that:

var particle;

And also that ir works well if I remove the line 42.

Why does it work like that? What's going on?

Thanks

Upvotes: 1

Views: 157

Answers (3)

PANKAJ
PANKAJ

Reputation: 88

'particles, particle' are declared in local scope in line 42 (continuation of line 41). However 'particles' is not used anywhere and 'particle' is used locally. It is always a good practice to declare variable local in javascript . Removing the line 42 makes 'particle' a global variable. However as 'particle' is not required outside the scope of function, it is better to declare it local within the function. Javascript is pretty straightforward. You can check references http://www.w3schools.com/js & http://www.javascriptkit.com

Upvotes: 0

Asherah
Asherah

Reputation: 19347

This code has been formatted poorly (imho). It's not made clear, but:

var mouseX = 0, mouseY = 0,

windowHalfX = window.innerWidth / 2,
windowHalfY = window.innerHeight / 2,

SEPARATION = 200,
AMOUNTX = 10,
AMOUNTY = 10,

camera, scene, renderer;

This is one single var statement, with the commas continuing it over lines. This may answer the confusion and help you rearrange declarations.

Upvotes: 1

Aatch
Aatch

Reputation: 1856

Line 42 is indeed

particles, particle;

however, line 41 contains

var container, separation = 100, amountX = 50, amountY = 50,

so the full statement is (on one line)

var container, separation = 100, amountX = 50, amountY = 50, particles, particle;

Not to be rude, but it might help to read the code a bit more carefully next time.

Upvotes: 0

Related Questions