flex-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alex Harui (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (FLEX-34897) Scope is wrong when calling member function from a variable reference
Date Fri, 21 Aug 2015 14:21:45 GMT

    [ https://issues.apache.org/jira/browse/FLEX-34897?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14706783#comment-14706783
] 

Alex Harui commented on FLEX-34897:
-----------------------------------

Better fix in flex-asjs 4dcbde2e87f41569de645f7b8c99e10cf8dfd2b9
and flex-falcon 1f9b06b4d32a4fe6fc82127a1cc858bb85a0082b


> Scope is wrong when calling member function from a variable reference
> ---------------------------------------------------------------------
>
>                 Key: FLEX-34897
>                 URL: https://issues.apache.org/jira/browse/FLEX-34897
>             Project: Apache Flex
>          Issue Type: Bug
>          Components: FalconJX
>    Affects Versions: Apache FalconJX 0.0.3
>            Reporter: Josh Tynjala
>            Assignee: Alex Harui
>
> In Flash Player, when you save a reference to a member function in a variable, 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
variable reference to the function 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 behave the same:
> 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?
> 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 in Flash Player makes
this easy by automatically binding all member functions to the instance. JavaScript usually
requires some manual intervention to get event listeners to be called with the right scope.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message