# Why use "this."?

**URL:** <https://forum.kirupa.com/t/why-use-this/27063>\
**Category:** Uncategorized\
**Created:** [July 29, 2003, 8:46pm UTC](https://forum.kirupa.com/t/why-use-this/27063 "2003-07-29T20:46:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NeoDreamer](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@NeoDreamer](https://forum.kirupa.com/u/NeoDreamer)\
**Post date:** [July 29, 2003, 8:46pm UTC](https://forum.kirupa.com/t/why-use-this/27063/1 "2003-07-29T20:46:07Z")

</div>

Why do you have to use “this”? If you leave it out, the script still works the same way.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 29, 2003, 8:51pm UTC](https://forum.kirupa.com/t/why-use-this/27063/2 "2003-07-29T20:51:38Z")

</div>

Good coding practice.

And when doing object oriented programing the difference of this and no this is sometimes integral. This being a good example of why…

[http://www.kirupa.com/developer/actionscript/tricks/scope.asp](http://www.kirupa.com/developer/actionscript/tricks/scope.asp)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 29, 2003, 8:59pm UTC](https://forum.kirupa.com/t/why-use-this/27063/3 "2003-07-29T20:59:17Z")

</div>

**this.myMovie.gotoAndPlay(1);** is the same thing as **myMovie.gotoAndPlay(1);**. Right?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 29, 2003, 9:01pm UTC](https://forum.kirupa.com/t/why-use-this/27063/4 "2003-07-29T21:01:17Z")

</div>

Yes, they are the same.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 29, 2003, 10:22pm UTC](https://forum.kirupa.com/t/why-use-this/27063/5 "2003-07-29T22:22:12Z")

</div>

> \*Originally posted by Supree \*  
> \*\***this.myMovie.gotoAndPlay(1);** is the same thing as **myMovie.gotoAndPlay(1);**. Right? \*\*  
> not all the time! You could have a myMovie inside another movieclip which this was called and you’d have to use this.myMovie…

You could also have a myMovie on the root in which case, myMovie…would refer to it.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 29, 2003, 10:31pm UTC](https://forum.kirupa.com/t/why-use-this/27063/6 "2003-07-29T22:31:59Z")

</div>

basically the difference lies in function calls

In coding normally directly within the scope of a timeline and not withn any function, this can be pretty much always left out. There are minor differences of using this and not using this but nothing to really worry about.

otherwise, its the function calls where this comes into play. In a function, ‘this’ refers to the object or timeline which the function exists. If you dont use this, then whatever variable you’re trying to get is checked in the timeline the function was written - and the timeline it was written is not always the timeline it exists. ex:

```auto
num = 5;
createEmptyMovieClip("myMC",1);
myMC.num = 10;
myMC.testFunc = function(){
	trace(num); // traces 5
	trace(this.num); // traces 10
}
myMC.testFunc();

```

you can see here two num variables. One in the timeline this code is being written, and one in the movieclip created within that timeline. That movieclip is then given a function. This function, though its assigned to exist in myMC, is, as you can see, being written in that current timeline. So, when the function is run, num is 5 - the value of num in the timeline the function was defined and this.num is 10 - the value of num in the object the function itself exists which is myMC.
