I am losing the plot trying to Google 'this'; it just isn't working. BUT I did find a description by looking at the ActionScript 3.0 documentation index. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/sta tements.html#this
Here it is:
"A reference to a method's containing object. When a script executes, the this keyword references the object that contains the script. Inside a method body, the this keyword references the class instance that contains the called method."
Soooo, to access a property or method from a method within the same class, you would type this: this.method() or this.property, right? I'm pretty sure it works without 'this.', but it's confusing me because I learnt PHP before ActionScript, and you always require 'this ->' in this circumstance.
Also, what the aubergine does 'this' refer to when used from the main timeline (not within a class)?
Post script:
Sorry about using the word 'this' so much……… this.
Another way of looking at "this" is to think of it as refering to whatever object contains the current timeline.
In AS3 the use of "this" when targeting is most often unnecessary. You could think of it as being undertsood by the compiler as being the current timeline scope. It most often comes into play when you use bracket (aka array) notation to target objects using Strings, such as this["some_instance_name_as_a_string"] since the bracket notation does require some containing object reference for targeting.
Thanks for the reply! I have no idea what it means for an object to 'contain' the timeline, but don't worry about answering that; I took from what you said all I needed, which was the fact that I won't be needing to use the 'this' statement to access other methods and properties from within the same class.
I'm not worried about answeing it... if you have a movieclip symbol in your library, it contains its own timeline - you can open it for editing and see it just like you see the main timeline. If you only work with class files, then chances are you don't deal with such animals, but they exist.
So which MovieClip contains the main timeline? Wait… what is the frame-contained ActionScript in the main timeline inside?
… In fact, forget answering those questions, because then I'll ask more, etc.
Post script (this isn't really related…):
I'm on a university course and I have been given a few weeks to learn ActionScript 3.0 from scratch; that isn't possible, so I have to create a programme for an assignment without actually learning… anything. There is a legitimate skill to doing that: it's called bullsh***ing. I suck at it; this is because I have two cognitive distortions called 'dichotomous (all or nothing/black and white/good or bad) thinking' and 'should statements' (thinking about how things should be, and not having the ability to accept how things actually are). So when it comes to learning a language, if I'm going to do that, I have to master it completely or not at all; not being able to do that irritates me and all I can think about is how the course isn't what it should be.
… And that is why I am having so much difficulty: I'm obsessive. :\
1. I think Ned's explanation linking this keyword to timeline is limiting because timline can be an attribute of MovieClip instance only whereas this keyword refers to ANY AS3 object's instance scope. Main clue is the word INSTANCE. So, this keyword has much wider meaning and usage.
2. Although this is true that one of the conveniences of this is to use it as an associative array, I wouldn't say it is the main usage. In my opinion, in well designed OOP applications the is not need for associative array syntax because there are many more efficient approaches to reference to properties.
"I won't be needing to use the 'this' statement to access other methods and properties from within the same class."
You should reconsider.
Here are examples when this is very useful:
A. Methods parameters vs instance properties/methods naming conventions.
In this example method's parameter and instance property have the same name but there is no confusion:
private var width:Number;
public function changeWidth(width:Number):void {
this.width = width;
}
B. Names of instance property and method's variable are the same. Again, references are clear:
private var myVariable:String;
private function doSomething():void {
var myVariable:String = "BLAH";
this.myVariable = "ANOTHER BLAH";
}
Also, although AS3 doesn't support overloading, one can call methods scoping them to either this or super.
override protected function myFunction():void {
}
private function callMyFunction():void {
this.myFunction();
super.myFunction();
}
In addition, when static vs instance propertied/methods are used - one cannot scope static ones to this which sometimes is useful as well.
North America
Europe, Middle East and Africa
Asia Pacific