three.js variable

Having difficulty understanding the current situation, it appears there may be a syntax problem. Just to provide some context, I am relatively new to Javascript but have previous experience with AS3.

Looking at this code:

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

Specifically, line 34 shows:

camera, scene, renderer;

Initially, I assumed these were global variable declarations and that in line 42, they were local declarations within the init() function.

Line 42 reads:

particles, particle;

Upon experimenting with the code, I noticed that declaring the particle object globally as shown caused the script to fail. However, it worked properly when declared as follows:

var particle;

In addition, removing line 42 also fixed the issue.

What is the reason behind this behavior? Can someone please explain what's happening?

Thank you!

Answer №1

The coding style of this snippet could use some improvement, in my opinion. While it's not explicitly mentioned, take note that:

let positionX = 0;
let positionY = 0;

const windowWidth = window.innerWidth / 2;
const windowHeight = window.innerHeight / 2;

const DISTANCE = 200;
const AMOUNT_X = 10;
const AMOUNT_Y = 10;

let camera, scene, renderer;

This entire block falls under a single let statement, utilizing commas to extend the declaration across multiple lines. Understanding this might assist in clarifying any confusion and organizing your variable assignments more effectively.

Answer №2

Line 42 is confirmed to be

particles, particle;

However, line 41 includes

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

Therefore, the complete statement is (written on a single line)

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

I don't mean to sound rude, but it may be beneficial to review the code more attentively in the future.

Answer №3

Local variables 'particles' and 'particle' are defined in line 42, but only 'particle' is utilized within the local scope. It's advisable to keep variables local in JavaScript for best practices. If line 42 is removed, 'particle' becomes a global variable. However, since 'particle' is not needed outside the function, it's preferred to declare it as a local variable within the function.

JavaScript is known for its simplicity. Additional references can be found at http://www.w3schools.com/js and http://www.javascriptkit.com

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible for jQuery to fail within an object method?

Consider this scenario: function Schedule (foo) { this.foo = foo; this.bar = function() { $.ajax({ url: '/something/', method: "GET", dataType: "JSON" }).done (function(data){ ...

Experiencing difficulties accessing Facebook using ngFacebook on angularjs application

I've been working on implementing ngFacebook login into my Angular app, but I'm facing an issue with logging in to Facebook. Even after calling '$facebook.log()', nothing is being displayed in the console. This is a snippet of my Angul ...

Show the present Pacific Daylight Time

The PDT offset can either be -7 or -8 hours depending on daylight saving time changes. I am unable to just subtract 7 * 60 * 60 from the UTC timestamp, as that seems to be the common suggestion when searching for a solution to this problem. Is there a str ...

Initiating Ajax to trigger the body's onLoad event

Whenever I use an ajax call to load a div, the entire page refreshes. It seems that the 'body onload=init()' event is triggered on ajax response causing all the initialization to repeat, which is not desired. Is there a way to only load the div t ...

What is the best way to integrate Angular 5 with various sources of JavaScript code?

I am fairly new to angular. I am looking to merge an external angular script file and on-page javascript code with angular 5. I understand that angular does not typically allow for the inclusion of javascript code in html components, but I believe there m ...

Encountering a syntax/parse error while utilizing jQuery within Velocity code

<script type="text/javascript> (function($, win) { function myCustomInit () { var self = this; var options = { 'params' : "userId=" + userId; }; self.init = function() { self.initButtonAction(); }; ...

Change the chosen item to uppercase within a dropdown menu using pure vanilla JavaScript

I am trying to achieve a specific effect where the text is uppercase only on the selected option within a <select> element. I came across a solution using jQuery in this article, but I need to convert it to plain JavaScript (vanilla JS). I have made ...

What is the best approach for connecting Advanced Custom Fields in WordPress to components in my Next.js frontend using GraphQL?

Currently, I am in the process of constructing a website by utilizing WordPress as the headless CMS and NextJs for my frontend. My dilemma lies in using the free version of ACF and wanting to dynamically query and map ACF to components in Next when generat ...

Sort activities according to the preferences of the user

This is the concept behind my current design. Click here to view the design When making a GET request, I retrieve data from a MongoDB database and display it on the view. Now, I aim to implement a filter functionality based on user input through a form. ...

Performing an AJAX request from a subfolder

Can anyone help me figure out how to make an ajax request from a page located in a subdirectory and receive a response? On my webpage, index.jsp is situated within a subdirectory called /vf2. In this page, there is a script file included: <script src=" ...

Tips on obtaining a Firebase ID

Does anyone have a solution for retrieving the unique id from Firebase? I've attempted using name(), name, key, and key() without any success. Although I can view the data, I'm struggling to find a way to retrieve the id. This information is cru ...

Transform your TypeScript code with a jscodeshift codemod that removes generic type declarations while preserving the wrapped

I am currently working on developing a codemod that will eliminate all instances of the $ReadOnly<T> generic from a TypeScript codebase, while retaining only T (where T represents an object or union). This is what I have managed to come up with so f ...

Code error detected on basic webpage

I've been experimenting with my local storage and have encountered a strange issue. The code example that I had previously tested successfully is now not working, and I can't figure out what went wrong. Check out this link for additional informa ...

Adding the Bootstrap script

I am facing an issue with incorporating a Bootstrap navbar into my page using a PHP function. I have already included the Bootstrap CSS file, but now I also need to add the script for a dropdown menu functionality. When I tried placing the script at the en ...

Using React to retrieve data from Firebase through axios

I recently uploaded some mp3 files to my Cloud Storage on Firebase. To retrieve these files, I am utilizing axios for the necessary operations. After modifying the permissions under the "rules" tab to: allow read, write, request; I am still encou ...

Button within the Magnific Popup (do not use to close)

I am currently developing a website with a gallery page that utilizes the Magnific Popup plugin. My client has provided lengthy 'captions' for each image, almost like short stories. To display these captions effectively, I have utilized the titl ...

problem when using directive $compile with encapsulating HTML

I'm currently working on developing a directive that, when declared like this: <input my-directive show-button-bar="true" ng-model="fooBar"\> It should generate the following HTML structure: <div on-toggle="toggled(open)" is-open="dpM ...

Strange occurrences observed on array mapping post-state alteration

Greetings to all during these challenging times! Currently, I am delving into Firebase and ReactJS and have encountered a peculiar issue involving state updates in React and the array map functionality in JavaScript. Below is the code snippet showcasing my ...

Information regarding gender vanishes upon refreshing the page

When the page is refreshed, the variable called jso disappears. Is there an alternative method for storing information that does not involve using a button? The goal is to have it work seamlessly when the page is reloaded without requiring any user action. ...

Using Reactjs to create a custom content scroller in jQuery with a Reactjs twist

I am attempting to implement the Jquery custom scrollbar plugin here in my React project. Below is a snippet of my code: import $ from "jquery"; import mCustomScrollbar from 'malihu-custom-scrollbar-plugin'; ..... componentDidMount: function() ...