flex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Schmalle <teotigraphix...@gmail.com>
Subject Re: [FalconJX] Scope when calling member function from a reference
Date Sun, 28 Jun 2015 21:21:46 GMT
On Sun, Jun 28, 2015 at 5:17 PM, Josh Tynjala <joshtynjala@gmail.com> wrote:

> Yes, that is correct.
>
> In case it wasn't obvious, event listeners are the typical use case where
> you'd pass a reference to a member function somewhere else where a
> reference needs to be saved in a variable. AS3 made this easy by
> automatically binding member functions. JavaScript usually requires some
> manual intervention to get event listeners to be called with the right
> scope.
>

Yeah the compiler does this already for anonymous functions, it creates a
self var pointing to 'this' and then uses self in the anonymous function's
body.

I wasn't aware of this problem though, can you create a JIRA ticket? I
probably will be the one that tackles it since I am sure Alex doesn't have
time to do something like this and test it as well.

Mike



>
> - Josh
>
> On Sun, Jun 28, 2015 at 2:06 PM, Michael Schmalle <
> teotigraphixllc@gmail.com
> > wrote:
>
> > So this only happens in javascript when you pass an object function to a
> > variable? SO what I am saying is that not using this.func() is what
> looses
> > the connection to the instance scope and then this becaomes window,
> > correct?
> >
> > Mike
> >
> > On Sun, Jun 28, 2015 at 4:48 PM, Josh Tynjala <joshtynjala@gmail.com>
> > wrote:
> >
> > > In Flash Player, when you save a reference to a member function, you
> can
> > > call the function reference, and "this" will still be bound to the
> > instance
> > > where it came from.
> > >
> > > public class Test
> > > {
> > >     public function Test()
> > >     {
> > >         this.func();
> > >         var func:Function = this.func;
> > >         func();
> > >     }
> > >
> > >     private function func():void
> > >     {
> > >         trace(this); //in Flash, "this" will always be an instance of
> > Test
> > >     }
> > > }
> > >
> > > Basically, in the code above, the two calls to func() will behave the
> > same
> > > in Flash Player. However, in the current implementation of the
> > transpiler,
> > > that behavior is lost. When the reference to func() is called, "this"
> > ends
> > > up referring to the global window object instead.
> > >
> > > JavaScript function objects have a bind() function that let's you set
> > what
> > > "this" will refer to when the function is called:
> > >
> > >
> > >
> >
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
> > >
> > > After modifying my code to use bind(), the two calls to func() will
> have
> > > the same output:
> > >
> > > public function Test()
> > > {
> > >     this["func"] = this.func.bind(this);
> > >     this.func();
> > >     var func:Function = this.func;
> > >     func();
> > > }
> > >
> > > Would it be possible for the transpiler to automatically bind all
> member
> > > functions to the correct scope to preserve the behavior that AS3
> > developers
> > > expect?
> > >
> > > - Josh
> > >
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message