Just installed the latest version of ThreeJS and now having trouble with the instanceof operator not functioning properly

After updating to r99, I've encountered an issue with my instanceof checks no longer working properly.

For example, when I traverse through an object and check if its children are instances of Mesh, it's returning false. However, if I inspect child.constructor.name, it shows as Mesh.

object.traverse(child => {
    console.log(child);
    if (child instanceof THREE.Mesh) {
        console.log('THREE.Mesh');
    } else {
        console.log('NOT THREE.Mesh');
    }
    console.log(child.constructor.name);
    console.log('----');
}

https://i.sstatic.net/gBxZf.png

What could have possibly gone wrong or changed when simply updating the library?

Answer №1

It's unclear how the object you're navigating through was generated without access to that code, but it's possible that the loader you're utilizing altered its approach.

When verifying types, I suggest utilizing the predefined .type property, which is what Three.js internally employs to distinguish between different object types. The issue with using instanceof is that it can lead to ambiguous outcomes when working with subclasses:

var myMesh = new THREE.Mesh();
console.log(myMesh instanceof THREE.Mesh);      // True
console.log(myMesh instanceof THREE.Object3D);  // Also true

However, by checking the .type property, you'll have more certainty about the object type.

console.log(myMesh.type === "Mesh");        // True
console.log(myMesh.type === "Object3D");    // False

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

Absence of information from the localStorage is evident

I am currently working on a project using ReactJs and utilizing a rich text editor. I have encountered an issue where the data stored in localStorage is not showing up in the content area after refreshing the page, even though I have successfully saved the ...

Automatically adjust the top margin of the content section to accommodate a fixed header height

After extensive searching, I've come across various solutions but none of them seem to fit my needs. I am a beginner/amateur developer. The issue I am facing is with a fixed header that resizes when scrolling. I understand that by adding padding-top: ...

Retrieving data objects from axios call in Vue and passing them to the controller

Currently, I am using Vue within Laravel and facing an issue with retrieving data from a controller function that I am accessing. My goal is to use this data in the data() section of my Vue template. Though I am aware that the controller function returns ...

Change the position of a div by clicking a button using JavaScript

I have a div with an image that I want to move by clicking on a button within the same div. The reason for this is that clicking on the image itself allows it to move inside the div, but I want another element to control the movement of the entire div. He ...

Which approach yields better performance: setting all dom events simultaneously, or incrementally?

Is it more effective to attach event listeners permanently or only when needed? For example, consider a scenario where you have a dropdown menu in your HTML that opens when clicked. Within this menu, there is a close button that closes the menu upon click ...

Breaking down strings using delimiters in JavaScript

Looking for a solution to parse a string with markers: 'This is {startMarker} the string {endMarker} for {startMarker} example. {endMarker}' I want to transform it into an array that looks like this: [ {marker: false, value: 'This is&ap ...

Updating the placeholder text of a textarea using a conditional statement in PHP

I currently have a textarea within a form that is being outputted: <textarea id='textarea' name='msg' rows='2' maxlength='255' cols='80' placeholder=' Share a thought...'></textarea> ...

I am looking to have the passport store req.client instead of req.user when using the Bearer strategy

Currently, I am setting up an Oauth2 bearer strategy for client authentication using passport.js. My implementation involves utilizing the passport-http-bearer package with the following callback: passport.use(new BearerStrategy( function (accessTok ...

"Incorporating Adobe Edge Animate alongside AngularJS and AngularUI Router for dynamic web design

Currently, I'm in the process of integrating multiple animations created using Adobe Edge Animate into a single AngularJS application for a project. These animations will serve as the visual elements for a game, with player input controlling the compo ...

The Express.js feature "app.use() mandates the use of middleware functions"

Currently, I am delving into learning Express.js 4 and Node, but I seem to have hit a roadblock with an error that has me stumped. I'm attempting to utilize the node-sass package to compile my sass code; however, I've encountered obstacles in ge ...

JavaScript Instant Validation: Ensuring Accuracy in Real-Time

I am trying to create a JavaScript code that validates user input based on certain rules. If the input does not meet the criteria, I want an error message to appear next to the text field. For instance, if the rule is that the first character must be a cap ...

Using Ajax with Controller Action in Yii2

As a newcomer to programming, I am facing an issue where I need to call a function when the user inputs data and clicks the submit button. Although I am working with Yii2, I lack experience in Ajax. Despite my efforts, the controller action is not being tr ...

Is there an issue with the onClick event in React Buttons when they are generated using the .map()

Is the button created in the .map() function expired after the function ends? It does not seem to work. The code reads from a JSON file that stores an array, loops through it to display items including the button. When clicked, the button should set the d ...

Displaying an alert when the text box contains a duplicate value

I have a set of input fields with various values that can be edited but must not be left empty (if left empty, display an alert). When I update a field with a new value, if that new value matches any of the other input field values, I want to display an al ...

What is the process for choosing and making changes to a specific portion of a textContent?

<h1 id="block ">This is my header block</h1> To dynamically change the color of the "block" word every 2 seconds using JavaScript: let colors = ["blue", "green", "pink", "purple"]; let curColor = 0; const changeColor = () => { ...

What could be causing my jQuery to only toggle the first arrow in my HTML?

I am facing an issue with a series of divs generated from data, each containing an arrow that should toggle an expandable section. Strangely, only the first div is functioning properly: toggling the arrow icon and revealing the content upon click. When th ...

Route Separation with ExpressJS and Socket.IO

As I delve into the world of ExpressJS and Socket.IO, I find myself facing a puzzling situation. My routes are neatly organized in a separate file that I include from my app.js: var express = require('express') , db = require('./db&ap ...

What steps can I take to address this Material UI alert and deliver a solution that adds value?

I am currently working on fetching API data (specifically category names) from the back-end (Node.js) to the front-end (React). My main objective right now is to populate a Select component from Material UI. To fetch the API data, I am utilizing Express an ...

Managing checkboxes using the useState object

I am currently working on displaying checkboxes using the .map() function to retrieve data from a JSON file. My current challenge is that when I click on a checkbox, it stores the clicked checkbox in the state object. However, if I click on another checkb ...