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