In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

How Do You Explain 'this'?

Get Adobe Flash player
by 9re 20 Aug 2010
  • Related works: 1
  • Talk

    bkzen at 19 Aug 2010 08:21
    global にはnumChildrenというプロパティもremoveChildAtというメソッドもありません。 globalはMediaRSSReaderというプロパティしかもっていないはずです。 クロージャの中でthisはglobalなのだからthisをつけた場合は上記のプロパティ以外全てundefinedです。 this をつけなかった場合のスコープはglobalではなく外側のMediaRSSReaderインスタンスをさすのではないでしょうか?そうでなければ_thisを参照することはできないはずです。
    bkzen at 19 Aug 2010 09:15
    間違えました >mediaRSSReaderインスタンスをさすのではないでしょうか? じゃなくて、 この場合はglobalを含むMediaRSSReader のコンストラクタ内を指すのではないでしょうか? が正しそうです。
    makc3d at 19 Aug 2010 11:28
    google translate doesnt quite handle these comments well, could you put it in english for the rest of us?
    imajuk at 19 Aug 2010 12:02
    >why numChildren and this.numChildren differs inside the closure? the closure doesn't have "numChildren" propety. the 2nd trace in closure doesn't tell specific scope, so Flash Player look for "numChildren" property in scope chain, and then it find the property in MediaRSSReader's scope. the 3rd trace tell specific scope as "this", so Flash Player looks for the property in not scope chain but only the closure's scope, as a result Flash Player prints "undefined" . >why numChildren is same outside and inside? it's same reason as above #see also http://www.imajuk.com/blog/archives/2008/04/post_4.html
    9re at 19 Aug 2010 18:38
    @imajuk thanks for the detail explanation!!

    Tags

    Embed
/**
 * Copyright 9re ( http://wonderfl.net/user/9re )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/pQXU
 */

// forked from 9re's forked from: forked from: forked from: Guess the tag!
// forked from mash's forked from: forked from: Guess the tag!
// forked from 9re's forked from: Guess the tag!
// forked from makc3d's Guess the tag!
package {
    import flash.display.Sprite;
    import com.bit101.components.PushButton;
    import com.actionscriptbible.Example;
    
    public class Main extends Example {
        public function Main() {
           /*
            * I know 'this' differs outside and inside of the closure.
            * But why numChildren and this.numChildren differs inside 
            * the closure?
            * Or why numChildren is same outside and inside?
            */
           
            var _this:Sprite = this;
            trace("[outside]this: " + this);
            trace("[outside]numChildren: " + numChildren);
            trace("[outside]this.numChildren: " + this.numChildren); 
            
            (function ():void {
                trace("[inside]this: " + this);
                trace("[inside]numChildren: " + numChildren);
                trace("[inside]this.numChildren: " + this.numChildren);
                trace(_this.removeChildAt === removeChildAt);
                trace(this.removeChildAt === removeChildAt);
            })();
        }
    }
}