[SOLVED] This.function is not a function

Hi, I’m almost sure this is a scoping issue but when I try to use other ways I Playcanvas just hangs so I was wondering if I could get some clarity.

I need the the function load_model to call the function populate_mattress_data when its done loading. With this code below, it just says that populate_mattress_data is not a function…

ModelScreenManager.prototype.populate_mattress_data = function() {
console.log("now we display the model");
}

ModelScreenManager.prototype.load_model = function(model_to_load) {
    
    var self = this;

    var onAssetReady = function(asset) {
        // Show the entity when the asset is loaded
       
        console.log("NOW SHOW ASSET "+model_to_load.name);
        this.populate_mattress_data();
    };
    
    // Start async loading the asset
    model_to_load.ready(onAssetReady);
    this.app.assets.load(model_to_load);
      
};

Hi @Grimmy,

You can easily do that by appending .bind(this) to your function:

    var onAssetReady = function(asset) {
        // Show the entity when the asset is loaded
       
        console.log("NOW SHOW ASSET "+model_to_load.name);
        this.populate_mattress_data();
    }.bind(this);
1 Like

This page explains the this scope quite well: this - JavaScript | MDN

Hmm, I tried that, but the whole of PLaycanvas (app, editor, code editor) just hangs.

My fault… infinite loop in the function call. Oops

1 Like