# What is 'this'? \[scope issue\]

**URL:** <https://phaser.discourse.group/t/what-is-this-scope-issue/4276>\
**Category:** Phaser 3\
**Created:** [November 16, 2019, 1:46pm UTC](https://phaser.discourse.group/t/what-is-this-scope-issue/4276 "2019-11-16T13:46:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![YanKleber](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yankleber/32/5152_2.png) [@YanKleber](https://phaser.discourse.group/u/YanKleber)\
**Post date:** [November 16, 2019, 1:46pm UTC](https://phaser.discourse.group/t/what-is-this-scope-issue/4276/1 "2019-11-16T13:46:32Z")

</div>

And here we go again with something very primary! I have the following test code…

```
var config = {
    width: 800,
    height: 600,
    type: Phaser.AUTO,
    parent: 'test',
    scene: {
        create: create
    }
};

var game = new Phaser.Game(config);

function create ()
{
    showText();
}

function showText()
{
   this.add.text(15, 15, 'test', { fontFamily: '"Arial"' });
}

```

Now debugger says “cannot read property ‘text’ of undefined” where it’s obvious to me that the issue is the scope (it doesn’t know what is ‘this’). I tried to replace ‘this’ with ‘window’ and ‘document’ but still no luck.

What the scope ‘this’ refers to here? And what would be the correct reference to use within functions?

Thanks!

---

<div class="post-metadata">

**Author:** ![prob](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/prob/32/628_2.png) [@prob](https://phaser.discourse.group/u/prob)\
**Post date:** [November 16, 2019, 2:08pm UTC](https://phaser.discourse.group/t/what-is-this-scope-issue/4276/2 "2019-11-16T14:08:14Z")

</div>

In your showText function, ‘this’ should be referring to the Scene, which is how you call/access most Phaser methods. ‘add’ is the Phaser GameObjectFactory, which is how you create game objects within that scene. If ever in doubt regarding context, use console.log.

In your example you could either pass the scene to your showText function, or make showText a method of the scene.

---

<div class="post-metadata">

**Author:** ![rexrainbow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/rexrainbow/32/51_2.png) [@rexrainbow](https://phaser.discourse.group/u/rexrainbow)\
**Post date:** [November 16, 2019, 2:10pm UTC](https://phaser.discourse.group/t/what-is-this-scope-issue/4276/3 "2019-11-16T14:10:01Z")

</div>

Or,

```javascript
showText.call(this); // this here is a scene instance

```

Reference: [Function.prototype.call()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call)

---

<div class="post-metadata">

**Author:** ![YanKleber](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yankleber/32/5152_2.png) [@YanKleber](https://phaser.discourse.group/u/YanKleber)\
**Post date:** [November 16, 2019, 2:47pm UTC](https://phaser.discourse.group/t/what-is-this-scope-issue/4276/4 "2019-11-16T14:47:39Z")

</div>

Thank you very much! Passing ‘this’ as parameter did the trick!

```
showText(this);

function showText(scope)
{
   scope.add.text(15, 15, 'test', { fontFamily: '"Arial"' });
}
```

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [November 16, 2019, 4:45pm UTC](https://phaser.discourse.group/t/what-is-this-scope-issue/4276/5 "2019-11-16T16:45:04Z")

</div>

[https://github.com/samme/phaser3-faq/wiki#how-do-i-use-this-in-a-scene](https://github.com/samme/phaser3-faq/wiki#how-do-i-use-this-in-a-scene)

For posterity.
