Can't access this.context... from inside of a GLTFLoader?

I seem to get errors when trying to access ‘this.context’ or anything under this. from within a GLTFLoader when trying to spawn objects

    public LoadFromUrl()
    {
        const loader = new GLTFLoader().setPath( 'https://github.com/KhronosGroup/glTF-Sample-Models/raw/master/2.0/DamagedHelmet/glTF-Binary/' );
        loader.load( 'DamagedHelmet.glb', function ( gltf ) {
            console.log(gltf);
            this.context.scene.add( gltf.scene );

        } );
    }

Original Post on Discord

by user 103054507105067008

I even tried declaring the context in start and reusing it within the loader, but the loader gets upset the moment I type ‘this.’

by user 103054507105067008

Thats a javascript binding issue again since you dont have this defined when you declare the function using function() { ...

Instead you can do it by adding your callback using the arrow syntax (gltf) => { then this is auto bound

Works great thanks, I will check the syntax next time I use three.js javascript sample code

by user 103054507105067008

Depending on what your intention is you can also declare the function on your class

private loaded(gltf) { ... and then add it to the load callback DmaagedHelmet', this.loaded.bind(this)) like so