This.app.fire causing a type error

so im working on a video game and this.app.fire causes a type error.
code snippet:

function save(){
        this.app.fire("save:collect_data");

heres the editor
https://playcanvas.com/editor/code/1057263?tabs=128035189&line=25&col=18&error=true

fixed by using a jerry rigged method of using a variable to trigger the call

This is due to scope issue. What you had before was declaring a function in a function:

// update code called every frame
SaveController.prototype.update = function(dt) {
    function save(){
        this.app.fire("save:collect_data");
    }
    // ...
};

In the function save, the this object is the scope of the function, not the script type due to how Javascript handles this and scope. (see You-Dont-Know-JS/ch1.md at 1st-ed · getify/You-Dont-Know-JS · GitHub)

You can get around this via a couple of different ways if you want to do something this in the future.

Create a script type function

SaveController.prototype.update = function(dt) {
    // ...
};

SaveController.prototype.save = function() {
        this.app.fire("save:collect_data");
};

And you can call the function as this.save()

Bind the scope of the function to another object

// update code called every frame
SaveController.prototype.update = function(dt) {
    function save(){
        this.app.fire("save:collect_data");
    }.bind(this);
    // ...
};

This makes the this object reference to the object you bound it to

Create a variable to reference the script type this outside the function

// update code called every frame
SaveController.prototype.update = function(dt) {
    var self = this;
    function save(){
        self.app.fire("save:collect_data");
    }
    // ...
};

Thanks for the non jerry rigged soloution.