The this keyword and its intricacies

A simple question to better understand the this Keyword in PlayCanvas.
In this code:

var MessageReceiver = pc.createScript('messageReceiver');

MessageReceiver.prototype.initialize = function () {
    // method to call when receiving message
    // the this keyword always point to the object calling the method
    console.log (this);
    var self = this;
    var onReceiveMessage = function() 
        {
        self.entity.enabled = false;
        console.log (this);
        };

In the first console.log, the this keyword points to the ScriptType object.
In the second console.log (inside OnReceiveMessage), the this keyword points to the Application object.
If that means that OnReceiveMessage is called by the Application object, why is it so?
Thanks.

It’s to do with object scope and closures

The sections here cover the area quite well:

https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/scope%20&%20closures/README.md#you-dont-know-js-scope--closures

https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/this%20&%20object%20prototypes/README.md#you-dont-know-js-this--object-prototypes

Thanks.

Will have a look.