Appendix D. Differences from ECMA-262 and JavaScript

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Part III:  Appendixes

Appendix D. Differences from ECMA-262 and JavaScript

Naturally, this book focuses on the intricacies of ActionScript, but if you own O'Reilly's well-known JavaScript: The Definitive Guide, you'll notice that the reference section of the two books share many similarities.

Although ActionScript, like JavaScript, is based on the ECMA-262 standard, certain differences were necessitated by the constraints of Player size and backward compatibility. If you're porting code from JavaScript, JScript, or another ECMA-262-based language, you'll find Table D-1 valuable. It summarizes the intentional differences between ECMA-262, JavaScript, and ActionScript (for Flash 5 and later). Likewise, if you're porting ActionScript to another language, you'll be better able to avoid the pitfalls caused by ActionScript's deviation from the ECMA-262 standard.

Table D-1 reflects the intentional differences between Flash ActionScript and the ECMA-262 standard. It does not reflect any bugs that may exist in its attempted implementation of the standard. Furthermore, Table D-1 reflects ActionScript's behavior when the #strict pragma is not being used. (The undocumented, unsupported #strict pragma was intended to enhance ActionScript's conformance with the ECMA-262 standard. However, the #strict pragma should not be used, because it crashes Internet Explorer for Windows. See the entry for the #strict pragma in the Language Reference for details.)

Table D-1. Differences between ECMA-262, JavaScript, and ActionScript

Topic

Description

Exception handling

ActionScript does not support the exception handling statements try/catch/finally or throw.

escape( ) encoding (Flash 6 only)

In Flash 6, for characters between code point 127 and 255 (high ASCII), ActionScript's escape( ) function encodes UTF-8 sequences using %xx rather than the method defined by the ECMA-262 specification. See escape( ) in the ActionScript Language Reference.

Statement labels

ActionScript does not support labeled statements, used by the break and continue statements to control program flow.

String-to-Boolean conversion

In ECMA-262, all nonempty strings convert to true. In ActionScript, only the strings that can be converted to a valid nonzero number convert to true.

Case sensitivity

The ECMA-262 specification demands complete case sensitivity. In ActionScript, keywords are case-sensitive but identifiers are not. See Chapter 15, especially Table 15-1.

Function scope (Flash 5 only)

In Flash 5 (but not Flash 6), when a function from one timeline is assigned to a variable in a different movie clip's timeline, the assigned function's scope chain changes to that variable's timeline. In ECMA-262 and Flash 6 ActionScript, it's impossible to modify a function's scope chain through assignment; scope is determined permanently by the location of the function declaration statement.

Regular expressions

ActionScript does not support regular expressions. However, developer Pavils Jurjans has made a custom RegExp object available at http://www.jurjans.lv/flash/RegExp.html.

Event handler properties (Flash 5 only)

In Flash 5 (but not Flash 6) movie clip event handlers must be defined using onClipEvent (eventName), and button event handlers must be defined using on (eventName). In Flash 6, clip and button event handlers can be assigned as object properties, as in JavaScript. See Section 10.5 in Chapter 10.

Global variables (Flash 5 only)

Flash 5 ActionScript does not support true document-wide global variables. Global variables can be simulated by attaching properties to Object.prototype, as described under Section 12.5.3.5. In Flash 6, global variables are created on the _global object (which itself is not part of ECMA-262).

The eval( ) function

ActionScript's eval( ) function supports a small subset of ECMA-262's intended functionality; it works only when its argument is an identifier and is used only to dynamically generate references to identifiers. In Flash 6, it can't be used on the left side of an assignment statement.

undefined datatype conversion

In ActionScript, the special undefined value converts to the empty string ("") when used in a string context and converts to the number 0 when used in a numeric context. In ECMA-262, undefined converts to the string "undefined" in string contexts and to the numeric value NaN in numeric contexts.

The Function constructor

ActionScript does not support the Function constructor, which is used in JavaScript to create functions with the syntax new Function ( );.

Date object creation

ActionScript will not accept (i.e., will not parse) a human-readable date string, such as "January 9, 2001", when creating a new Date object.

switch statement (Flash 5 only)

Flash 5 ActionScript does not support the switch/case/default statements (used to phrase complex conditionals). The switch statement is supported in Flash 6 and can be used when exporting from the Flash MX authoring tool to a Flash 5 or Flash 4 format .swf file.

Language support (Flash 5 only)

ECMA-262 requires support of the Unicode character-encoding standard, which Flash 5 ActionScript does not support. Flash 5 uses the Latin 1 and Shift-JIS character sets and implements a subset of Unicode-style functions and conventions (such as \u escape sequences). Flash 6 fully supports Unicode.

Object model

Naturally, JavaScript includes built-in classes and objects that relate to web browsers, whereas Flash includes those that relate to Flash movies. For JavaScript programmers who are used to working with DHTML, it may be helpful to think of the main movie of a Flash document as analogous to an HTML document object and movie clips as analogous to layer objects.

Timed code execution (Flash 5 only)

In Flash 5, the setTimeout( ) and setInterval( ) methods of the JavaScript window object are not available in ActionScript but can be simulated with timeline and clip event loops, as discussed in Chapter 8. Flash 6 supports setInterval( ), but not setTimeout( ).

Object constructor

In Flash 5 ActionScript, the constructor for the Object class does not accept any parameters. In ECMA-262 and Flash 6 ActionScript, Object accepts a value parameter, which can be a Boolean, string, or number primitive.

Slash and Tell Target notation

ActionScript supports legacy slash notation for composing variable references and controlling movie clips via Tell Target (e.g., "/ball:speed "). Neither Tell Target nor slash notation are defined by ECMA-262.

Nonstandard event handlers on( ) and onClipEvent( )

ActionScript supports a non-EMCA-262 event handler format for buttons (on( )) and movie clips (onClipEvent( )), used to attach event handlers directly to objects at authoring time. See Chapter 10.

arguments.caller

The arguments.caller property is supported by ActionScript, but it was deprecated from JavaScript in version 1.3.

In Netscape 4, JavaScript's arguments.caller property refers to the Arguments object of the calling function. In ActionScript, the arguments.caller property is a reference to the calling function itself, not the caller's Arguments object. See arguments.caller in the Language Reference for details.

Undeclared variables

In JavaScript, a reference to an undeclared variable causes a runtime error. ActionScript leniently permits undeclared variable references.

_ _proto_ _ property

ActionScript formally documents the Object._ _proto_ _ property, which is not exposed by ECMA or JavaScript. See Object._ _proto_ _ in the ActionScript Language Reference.

Invoking undeclared functions

In JavaScript, invoking an undeclared function causes a runtime error. In ActionScript, it fails silently.

constructor property

In JavaScript (in accordance with ECMA-262), the constructor property is always read from an object's constructor's prototype property (someObject._ _proto_ _. constructor). In Flash Player 6 and Flash Player 5, the constructor property is written directly to someObject. For a complete discussion of this subtle deviation, see Section 12.7.3.

Object.toString( )

ECMA-262 states that, for built-in classes, toString( ) should return: [object class], where class is the name of the object's class. In ActionScript, toString( ) returns [object Object] unless the class implements custom toString( ) behavior (e.g., the Date object returns a human-readable date).

super "operator"

ActionScript's super "operator" is not an implementation of ECMAScript v4's super operator. See Section 5.12.8.

     


    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net