# The this keyword and its intricacies

**URL:** <https://forum.playcanvas.com/t/the-this-keyword-and-its-intricacies/28635>\
**Category:** Help & Support\
**Created:** [November 30, 2022, 12:20pm UTC](https://forum.playcanvas.com/t/the-this-keyword-and-its-intricacies/28635 "2022-11-30T12:20:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayypeak](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/jayypeak/32/20505_2.png) [@jayypeak](https://forum.playcanvas.com/u/jayypeak)\
**Post date:** [November 30, 2022, 12:20pm UTC](https://forum.playcanvas.com/t/the-this-keyword-and-its-intricacies/28635/1 "2022-11-30T12:20:48Z")

</div>

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

```javascript
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.

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [November 30, 2022, 12:46pm UTC](https://forum.playcanvas.com/t/the-this-keyword-and-its-intricacies/28635/2 "2022-11-30T12:46:49Z")

</div>

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/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](https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/this%20&%20object%20prototypes/README.md#you-dont-know-js-this--object-prototypes)

---

<div class="post-metadata">

**Author:** ![jayypeak](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/jayypeak/32/20505_2.png) [@jayypeak](https://forum.playcanvas.com/u/jayypeak)\
**Post date:** [November 30, 2022, 1:02pm UTC](https://forum.playcanvas.com/t/the-this-keyword-and-its-intricacies/28635/3 "2022-11-30T13:02:19Z")

</div>

Thanks.

Will have a look.
