Inherit from Three.js classes

Hello :slight_smile:

For my project I want to create a new class that inherits from a Three.js class, so that can I add some more information to the initialized objects.

I am coming from the C# world and and when I am trying to convert the learnings from there I’d came up with this.

The new class:

//Beam class
class Beam extends THREE.Line3 {

    constructor(line, type, story, load, area){
        this.line = line;
        this.length = line.distance();
        this.type = type;
        this.story = story;
        this.load = load;
        this.loadedLength = load*length
        this.area = area;
    }
}
var testLine = new THREE.Line3(new THREE.Vector3(0,0,0), new THREE.Vector3(6,0,0))
var beam = new Member.Beam(testLine, "primary", 2, 40, 0.15)

console.log(beam)

But that does not work since I always get errors like this:

Unbenannt

Maybe someone could help me out?

Would be very appreciated! :slight_smile:

it seems you are mixing a few concepts here. I’m not sure what you actually want in the long run.
but in your example the extends THREE.Line3 is serving no purpose, since you are not calling super() in the constructor and you are have actually already created the THREE.Line before you call new Beam()

This maybe is what you are intending to do.

class Beam extends THREE.Line3 {
    constructor(start, end, type, story, load, area) {
        super(start, end);
        this.length = start.distanceTo(end);
        this.type = type;
        this.story = story;
        this.load = load;
        this.loadedLength = load * length;
        this.area = area;
    }
}
const beam = new Beam(new THREE.Vector3(0, 0, 0), new THREE.Vector3(6, 0, 0), 'primary', 2, 40, 0.15);
console.log(beam);

So now when you console.log your beam, you get all the properties expected of a line, plus your custom additions (type, story, etc)

image

Perhaps extending is not what you are really after since in your example code you have already created the line first before you create your beam. Maybe you just want your beam to have a property that is the line. It is not necessary to extend in that case.

1 Like

@seanwasere Awesome, thank you for taking the time this was exactly what I needed :pray:

1 Like