Modifying the temp variable by assigning a new value to this.value in Javascript

Using a temporary variable, I initialize a THREE.Vector3 which is then passed into the creation of an object.

object[i] = new Object(new THREE.Vector3(0,0,0));

Within the Object class, there is a variable called texture that gets assigned the new THREE.Vector3 named pos.

function Object(pos){
    this.texture = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map:
    THREE.ImageUtils.loadTexture( '../img/img.png' ) } ) );

    this.texture.position = pos;

    this.texture.rotation.x = 0;
    this.texture.rotation.y = 1.57;
    this.texture.rotation.z = 1.57;

    this.texture.scale.x = 100;
    this.texture.scale.y = 50;
    this.texture.scale.z = 100;

    this.texture.position.y = 1000;
}

The issue arises when trying to pass pos into another object created within this Object.

object2 = new Object2(pos);

The problem is that pos has been altered to have y-coordinate set at 1000. The reason behind this modification is unclear and after going through some guides on passing variables, confusion still remains regarding the alteration of the pos variable. Any assistance in resolving this matter would be greatly appreciated.

Answer №1

To ensure accuracy and consistency, it is recommended to always clone the position of your object before making any changes.

function Object(pos){
    this.texture = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map:
    THREE.ImageUtils.loadTexture( '../img/img.png' ) } ) );

    this.texture.position = pos.clone();

    this.texture.rotation.x = 0;
    this.texture.rotation.y = 1.57;
    this.texture.rotation.z = 1.57;

    this.texture.scale.x = 100;
    this.texture.scale.y = 50;
    this.texture.scale.z = 100;

    this.texture.position.y = 1000;
}

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

Trigger the OnAppend event for a jQuery element upon its insertion into the DOM

After writing a code snippet that generates custom controls, I encountered an issue where the custom scrollbar was not being applied because the element had not yet been appended to the DOM. The code returns a jQuery element which is then appended by the c ...

Using the `forEach` method nested within another `forEach

I'm facing an issue with the useEffect hook where it seems to not be rendering correctly. My cardArticle is only concatenating the last array and that's it. Below is the code snippet: const [articles, setArticles] = useState([]); const [cardA ...

Transforming a Python list into a JavaScript array

Hey there, I'm in the process of creating a JavaScript array of dates to input into a jQuery datepicker addon. Here is my Django view: def autofill_featured(request): show_id = request.GET.get('show_id') show = Show.objects.get(id=s ...

Engaging in payment processing

Currently facing a significant dilemma. I am utilizing Stripe as my payment gateway, which functions in a two-step process: Collect billing information to generate a token Charge the client using the generated token The issue arises because the token ca ...

Specify the dependencies in the package.json file to ensure that the React package designed for React v17 is compatible with React v18 as well

Recently, I developed an npm package using React v17.0.2. The structure of the package.json file is as follows: { "name": "shoe-store", "version": "0.1.0", "private": true, "dependencies": ...

What action is triggered on an Apple iPhone when a notification is tapped?

Currently, I am working on a React application and testing it on an Apple mobile phone. One issue I encountered is that when I receive an SMS, the number appears as a suggestion above the keyboard. I want to be able to tap on this number and have it automa ...

Error: The URL provided to the AngularJS factory is not properly formatted

I am facing an issue with passing a URL from a controller to a factory in my service. I have multiple URLs and want to dynamically pass any URL. var youtubeURL = 'https://www.googleapis.com/youtube/v3/videos?part=snippet'; ConnectivityS ...

Is it necessary to include a module in another module if it is not utilized in the template?

Is it necessary to import Module2 into Module1 if a component from Module2 is being used in Module1, but only in the typescript and not the template? For instance, as a @ContentChild(Component2) component2 like shown below (Note: both modules are secondary ...

Unable to detect errors using React/Redux

I encountered a login error handling issue with redux that is structured like this: export const login = (params: any) => async (dispatch: Dispatch) => { try { const authData = await API.post("login", params); sessionStorage.setIt ...

"An error message is displayed stating that the maximum call stack size has been exceeded while looping through an

Dealing with an array containing 10,000 items and attempting to assign a new property to each item has been a challenge for me. _async.mapLimit(collection, 100, function (row, cb){ row.type = "model"; cb(null, row); }, function (err, collect ...

default radio option with pre-selected value

Below is a radio option list, but I'm having trouble getting the desired default selection on first render: <label class="radio normalFontWeight"> <input type="radio" name="Criteria" data-ng-value="EVERYONE_REVIEWED" data-ng- ...

GraphQL and Relay.js issue: Error Message: Field "id" is expected in "Node"

It appears that the discrepancy lies in naming conventions between my schema.js file and the database field. The 'id' field in the schema is actually named differently in the database. var classroomType = new GraphQLObjectType({ name: 'Cl ...

Utilizing the map() function to iterate through a list of outcomes and assigning the resulting array as the state of a component in ReactJS

Currently, I am facing an issue with assigning an array value to the state in my react project. In order to do this, I have initialized my state as follows: constructor(props) { super(props); this.state = { category: [] } } My objec ...

Pop-up message on card selection using JavaScript, CSS, and PHP

I have a total of 6 cards displayed in my HTML. Each card, when clicked, should trigger a modal window to pop up (with additional information corresponding to that specific card). After spending a day searching for a solution online, I've come here s ...

What is the best way to set up an on-change listener for material-ui's <CustomInput...>?

I'm currently utilizing the React dashboard created by Creative Tim. I have a question regarding how to set up an onChange listener for a Here is the code snippet for the custom input class: import React from "react"; import classNames from "classna ...

Ensure that only a single onmouseover event is triggered when hovering over multiple elements

I want to create a simple code snippet like the one below: <span onmouseover="alert('hi')">Hello, <span onmouseover="alert('hello')">this</span> is a test</span> However, I need to ensure that when hovering ove ...

The visualization rendered by ChartJS is not displaying correctly when data is fetched using an AJAX call

When it comes to creating visualizations from static datasets using ChartJS, I have no problems. However, the issue arises when I try to use data fetched via an AJAX call - the visualization does not render properly. Surprisingly, no errors are thrown even ...

What is the best method for removing table rows with a specific class?

I have an html table with several rows, and for some of these rows the class someClass is applied. My question is: How can I delete the rows that have a specific class? <table> <tr class="someClass"> ...</tr> <tr class="someClass"> ...

Jquery on method triggers a single event only

I am encountering an issue with adding and removing a class from an element using the on() method to handle click events. The code works fine on the first click, but subsequent clicks do not trigger the event anymore. Here is the code snippet: $(&apo ...