Enhancing JavaScript Variables

It came to my attention that in ThreeJs you do the following:

    var obj = new THREE.BoxGeometry();
    scene.add(obj);
    obj.position.z += 23.5;

Upon passing the cube object into the scene along with all its vertices, you then modify its variables and scene recognizes the new coordinates. How is this achieved without having to pass obj back into scene?

Answer №1

The reason behind this occurrence is due to passing the object a by reference to the scene.add method. This means that any changes made to a will be reflected in the scene object as well.

You can test this behavior yourself by running the following code in your JavaScript console:

var a = {foo: 'bar'}, scene = {
    add: function(obj) {this.obj = obj},
    print: function() { console.log(this.obj.foo);}
});
scene.add(a);
scene.print(); // outputs bar
a.foo = 'baz';
scene.print(); // outputs baz

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

Converting a three.js scene into SVG or alternative vector format for export

Can an SVG- or other vector-formatted image be exported from a scene rendered using three.js's WebGLRenderer? What about from a scene derived from CanvasRenderer? If not, how can one set up SVGRenderer with three.js? Creating a new THREE.SVGRenderer( ...

Discovering Constraints on File Size: API Response Encoded in Base64 Visible Within 1 Megabyte, Overcoming Rendering Obstacles

Currently, I am encountering a hurdle in my Next.js application when trying to upload images larger than 1 MB. While the app works seamlessly with smaller images, it faces challenges when dealing with larger ones. Initially, there was a browser console er ...

Service running in the background in React Native

In the process of creating a React Native application, I am looking to retrieve data from the web every quarter of an hour. The method I choose should be capable of fetching data in the background while being compatible across multiple platforms. Is ther ...

Is there a way to generate buttons for values that are stored within an array, which is nested inside another array?

I am currently facing difficulties in creating buttons for values within an array, which are also nested inside another array. As an example: let numbers = [{"allNum": ["0", "1", "2", "3","4"]}, { ...

Enhance the "text" attribute of IXMLDOMElement to enable functionality in Chrome

The web application I am currently working on was developed approximately 10 years ago and is only compatible with Internet Explorer. My goal is to make it functional in Chrome as well. I am facing a challenge regarding the "text" property of IXMLDOMEleme ...

What could be causing the issue with my Mongoose One-To-Many Relationships not linking correctly?

Can anyone shed light on why the connection between "users" and "posts" (where users can have multiple posts) is failing to work properly? Despite setting up my mongoose associations correctly, when a new post is made, it doesn't get assigned to a use ...

Tried utilizing express to log URL parameters, but to no avail as it did not show any output on the console

Whenever I enter /posts/whatever at the end of the root URL, it doesn't log what comes after /posts to the console. app.get("/posts/:userId", function(req, res) { console.log(req.params.userId); }); Update: Here's the complete code: ...

Extract data from recurring rules

I am dealing with an object that is structured like this: const input = { "recurrence": { "rrule": [ "RRULE:FREQ=DAILY;UNTIL=20230606T133000Z" ], } }; My goal is to extract the value stored in FREQ and determ ...

Does the onchange function in the dropdown list only work when the first item is changed?

Here is a snippet of my HTML code featuring a list item generated from a database using a foreach loop: <select class="form-control select" id="inventoryitem" name="inventoryitem" onchange="getunit();"> <option>---Select an item---</o ...

Toggle visibility within the map() function in React

Currently, I am facing an issue with the collapse feature on the map. When I click to open a card, it opens correctly. However, when I try to open another card, it closes the previously opened card before opening the new one. What I actually want is to kee ...

React fails to respond to the .click() method

I believe the .click function originates from jQuery, but I came across it being used in pure JavaScript as well. This makes me wonder if I can utilize it in my React code too. The scenario where I want to incorporate it is as follows: keyUpFunction(even ...

Accessing the user's referral URL in Express.js when they submit a post

I am looking to implement a commenting system on my website, and I need to capture the page information where the comment is being made (such as a specific video uploaded by a user) in order to save it in the database. The route structure is set up like t ...

JavaScript program to split an array of strings into sets of arrays based on alphabetical order

I am looking to split an array of strings into multiple arrays based on alphabetical order. Can you help me achieve this? For example: let array = ['cheese', 'corn', 'apple', 'acorn', 'beet', 'banana ...

Encountering a JavaScript runtime error while trying to access and interpret JSON

Currently, I'm facing a challenge with converting a C# list of string types into a JSON object. The issue arises when trying to read this JSON object later in JavaScript. On the other hand, the process seems to work fine when dealing with a C# list of ...

Having trouble generating a WebGL context. Three js compatible with Chrome?

I encountered this error: Error creating WebGL context. Uncaught TypeError: Cannot read property 'getExtension' of null The issue is occurring on Google Chrome version 71. Strangely, when I open the developer tool and refresh the page, it works ...

No matter the way I input the URL in the AJAX call, the response always comes back as successful

I ran into an issue with my ajax request. It was supposed to be a GET request that returned some data, but no matter how I configured the URL, it always gave a success response even when it didn't actually succeed. var id = 5; $.ajax({ type: ...

Upload an image converted to `toDataURL` to the server

I've been attempting to integrate the signature_pad library, found at signature_pad, but I am struggling to grasp its functionality. Can someone guide me on how to retrieve an image value and send it to my server? Edit: I have experimented with dec ...

Tips for verifying the background color on a webpage with Robot Framework

Seeking assistance on validating the background color in a webpage using robot framwork. Can someone provide guidance or tips? ...

Issue encountered while retrieving information from JSON structure

My data is stored in a JSON file named info.json. [ {"employee": {"name":"A", "salary": "324423"}}, {"employee": {"name":"B", "salary": "43111"}}, {"employee": {"name":"C", "salary": "43434"}}, {"employee": {"name":"D", "s ...

Add to the current values of the REACT Form template property

I am new to working with REACT and I have been exploring whether it is possible to append a REACT Form control property value in order to enhance its functionality. To streamline the validation process, I have created a validation template that leverages ...