Skip navigation
Currently Being Moderated

I'm embarrassed to even ask this, but: what does 'this' refer to?

Nov 20, 2011 4:19 AM

Tags: #this

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.

 
Replies
  • Currently Being Moderated
    Nov 20, 2011 4:45 AM   in reply to Samuel Lockyer

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 20, 2011 6:37 AM   in reply to Samuel Lockyer

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 20, 2011 6:52 AM   in reply to Samuel Lockyer

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 20, 2011 7:57 AM   in reply to Andrei1

    My reference to the timeline was because his posting was asking about the main timeline.  "what the aubergine does 'this' refer to when used from the main timeline"  The other aspect of this was covered qute well by the quote he pulled from the documentation

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points