Maybe I'm missing something, but why are you worried about it being called
more than once?

What scenario requires an instance initializer instead of code in the
constructor?

-Alex

On 7/17/16, 9:59 PM, "Harbs" <harbs.li...@gmail.com> wrote:

>Well, one simple fix might be to move all functions before variables.
>This should work because functions are not evaluated until they are run.
>
>I think there is a bigger issue which I’m running into in other classes:
>I’m getting all kinds of errors which I think are due to the order of
>classes being loaded. During class initialization, I’m getting errors
>that other classes are not found.
>
>While thinking abut this, I came to the conclusion that class
>initializers and instance initializers are probably the way to go. The
>initializer functions can reassign themselves to do nothing to ensure
>they are only run once.
>
>Something like this:
>
>MyClass._init_class_ = function(){
>//Do class initialization stuff including variable and constant assignment
>       console.log("running init");
>       MyClass._init_class_ = function(){
>               // do nothing
>       }
>}
>MyClass(){
>       MyClass._init_instance_();
>}
>MyClass._init_instance_ = function(){
>// call _init_instance_ on super
>// assign the prototype
>MyClass._init_instance_ = function(){
>}
>
>}
>
>I’m not positive, but I’m pretty sure that this would eliminate almost
>all my issues.
>
>On Jul 18, 2016, at 7:27 AM, Alex Harui <aha...@adobe.com> wrote:
>
>> 
>> 
>> On 7/17/16, 11:48 AM, "Harbs" <harbs.li...@gmail.com> wrote:
>> 
>>> The following code (from Base64):
>>>             private static const _encodeChars:Vector.<int> = 
>>> InitEncoreChar();
>>>             private static function InitEncoreChar():Vector.<int>
>>>             {  
>>>             }  
>>> 
>>> Trying to run this code in javascript will result in an error that
>>> com.sociodox.utils.Base64.InitEncoreChar is not defined. Of course this
>>> makes sense based on how the javascript is compiled. (There’s no
>>> difference between a private static and a public static other than the
>>> comments.)
>>> 
>>> It’s simple enough to fix this by placing functions before variable
>>> assignments.
>>> 
>>> However, the same code works in Flash no matter which order it’s
>>> declared. That’s probably because the code is analyzed before it’s
>>> actually run.
>> 
>> I think it is because functions/methods are created in the class traits,
>> which run before the class initializer.
>> 
>>> 
>>> The thing is, that Javascript has the same functionality:
>>> 
>>> /**
>>> * @constructor
>>> */
>>> com.sociodox.utils.Base64 = function() {
>>>     com.sociodox.utils.Base64._encodeChars = InitEncoreChar();
>>>     function InitEncoreChar(){
>>>       var /** @type {Array} */ encodeChars = new Array(64, true);
>>>       var /** @type {string} */ chars =
>>> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
>>>       for (var /** @type {number} */ i = 0; i < 64; i++) {
>>>         encodeChars[i] = chars.charCodeAt(i);
>>>       }
>>> return encodeChars;
>>>             
>>>       }
>>> };
>>> 
>>> This will work no matter which order you declare the variable and the
>>> function because function declarations are evaluated before code is
>>> executed.
>>> 
>>> I don’t know a way to make this work in JS for public static functions,
>>> but for private static functions, this is likely a better pattern. It
>>> also makes the functions truly private…
>>> 
>>> Of course, I have no idea how much work it would be to make FalconJX be
>>> able to do this…
>> 
>> I'm not sure it is worth generating this fancier output, but folks are
>> welcome to create an emitter that does.
>> 
>>> 
>>> Thoughts?
>> 
>> We should fix this someday (probably by creating a class initializer
>> function), but since the workaround is simple enough, I'd say file a bug
>> but defer it to a future release.
>> 
>> -Alex
>> 
>

Reply via email to