Appendix B. ECMAScript

Table of Contents

1. Including Script
2. Differences between ECMA-262 and LZX
3. Reserved Words

LZX uses JavaScript (ECMAScript, as defined by ECMA-262, edition 3), as a foundation for its scripting syntax. For more information please visit: http://www.ecma-international.org/.

1. Including Script

Script can go in four places in an LZX application:

  • Inside an event attribute such as onclick.

    <canvas height="30">
      <button onclick="animate('x', 10, 1000, true)">Click</example>
    </canvas>
    
  • In a <method> or <script> tag.

  • In an attribute with type expression.

  • Inside a constraint expression.

  • In a separate file that is included via the src attribute of the <script> tag.

    <canvas>
      <include href="script.js"/>
      <!-- — -->
    </canvas>
    

2. Differences between ECMA-262 and LZX

The syntax and behaviour of LZX script is intended to be as specified by the ECMA-262 standard. However, there are some differences to be aware of, especially in the SWF runtime.

Exceptions

There are no exceptions, and no exception handling in the SWF runtime. try/catch/finally is not supported.

Errors such as 1/0, u(), k(), u.p, and o.u (where u is an undeclared variable, k is defined but isn't a function, and o is an object with property p) are silently ignored in the SWF runtime. (If debugging is enabled, all but the first of these will generate a warning message in the debugger window when evaluated.)

The best practice for detecting program errors in LZX code is to develop with debugging enabled (Chapter 50, Debugging) and to write unit tests (Chapter 51, Unit Testing).

The best practice for non-local transfer of control is to return a distinguished value or set a state variable.

ECMAScript Objects

Some ECMAScript objects and properties are not supported. See Table B.1, “Supported ECMAScript Properties” for a list of supported objects.

Browser Objects

ECMA-262 is NOT the same as client-side JavaScript as implemented by web browsers. This means that the objects that are browser specific (e.g. window, document, etc.) are not available to the LZX developer.

Forward references

Forward references to functions are not supported.

LZX does not support the ECMAScript objects, attributes and methods presented in this font in the following table in the SWF runtime.

Table B.1. Supported ECMAScript Properties

ECMA Object Properties Methods

Arguments

callee

length

 

Array

Array.prototype

constructor

length

Array(...)

new Array(...)

concat(...)

join(separator)

pop()

push(...)

reverse()

shift()

slice(start, end)

sort(comparefn)

splice(start, delete, ...)

toLocalString()

toString()

unshift(...)

Boolean

Boolean.prototype

constructor

Boolean(value)

new Boolean(value)

toString()

valueOf()

Date

Date.prototype

constructor

Date(...)

new Date(ms)

new Date(...)

new Date(string)[a]

parse(string)[a]

Date.UTC(...)

get[UTC]Date()

get[UTC]Day()

get[UTC]FullYear()

get[UTC]Hours()

get[UTC]Millisconds()

get[UTC]Minutes()

get[UTC]Month()

get[UTC]Seconds()

getTime()

getTimezoneOffset()

getYear()

set[UTC]Date(date)

set[UTC]FullYear(year[, month[, date]])

set[UTC]Hours(hours[, min[, sec[, ms]]])

set[UTC]Milliseconds(ms)

set[UTC]Minutes(minutes[, sec[, ms]])

set[UTC]Month(month[, date])

set[UTC]Seconds(sec[, ms])

setTime(ms)

setYear(year)

todateString()

toGMTSting()

toLocaleDateString()

toLocaleString()

toLocaleTimeString()

toString()

toTimeString()

toUTCString()

valueOf()

Error

Error.prototype

constructor

message

name

Error(message)

new Error(message)

toString()

EvalError

EvalError.prototype

constructor

message

name

EvalError(message)

new EvalError(message)

toString()

Function

Function.prototype

constructor

length

Function(...)

new Function(...)

apply(this, args[])

call(this, ...)

toString()

Global

Infinity

NaN

undefined

decodeURI()

decodeURIComponent()

encodeURI(uri)

encodeURIComponent(component)

escape(string)

eval(string)[b]

isFinite(number)

isNaN(number)

parseFloat(string)

parseInt(string[, radix])

unescape(string)

Math

Math.E

Math.LN2

Math.LOG2E

Math.PI

Math.SQRT1_2

Math.SQRT2

abs(x)

acos(x)

asin(x)

atan(x)

atan2(x, y)

ceil(x)

cos(x)

exp(x)

floor(x)

log(x)

max(...)[c]

min(...)[c]

pow(x, y)

random()

round(x)

sin(x)

sqrt(x)

tan(x)

Number

Number.prototype

Number.MAX_VALUE

Number.MIN_VALUE

Number.NaN

Number.NEGATIVE_INFINITY

Number.POSITIVE_INFINITY

constructor

Number(value)

new Number(value)

toString()

toLocalString()

toFixed()

toExponential()

toPrecision()

valueOf()

Object

Object.prototype

constructor

Object()

Object(value)

new Object()

hasOwnProperty(v)

isPrototypeOf(v)

propertyIsEnumerable(v)

toLocalString()

toString()[d]

valueOf()

RangeError

RangeError.prototype

constructor

message

name

RangeError(message)

new RangeError(message)

toString()

ReferenceError

ReferenceError.prototype

constructor

message

name

ReferenceError(message)

new ReferenceError(message)

toString()

RegExp

RegExp.prototype

constructor

global

ignoreCase

lastIndex

multiline

source

RegExp(pattern, flags)

new RegExp(pattern, flags)

exec(string)

test(string)

String

String.prototype

constructor

length

String(value)

new String(value)[e]

String.fromCharCode(...)

charAt(pos)

charCodeAt(pos)

concat(...)

indexOf(searchstr[, start])

lastIndexOf(searchstr[, start])

localeCompare(that)

match(regexp)

replace(searchstr, replacement)

search(regexp)

slice(start[, end])

split(delimiter)

substring(start[, end])

toLocaleLowerCase()

toLocaleUpperCase()

toLowerCase()

toString()

toUpperCase()

valueOf()

SyntaxError

SyntaxError.prototype

constructor

message

name

SyntaxError(message)

new SyntaxError(message)

toString()

TypeError

TypeError.prototype

constructor

message

name

TypeError(message)

new TypeError(message)

toString()

URIError

URIError.prototype

constructor

message

name

URIError(message)

new URIError(message)

toString()

[a] new Date(string) and Date.parse() are not supported. Passing a string to new Date(datestring) is not supported. The best practice is to construct dates from date components, for example: in place of new Date("7/20/1969") use new Date(1969, 7, 20).

[b] The eval() function cannot be used to execute arbitrary code. It may only be used only to get the value of an identifier named by a String.

[c] Math.max() and Math.min() only support two arguments.

[d] Object.toString always returns "[object Object]".

[e] Using new String(value) to clone a wrapper is not supported.


3. Reserved Words

Reserved words may not be used as identifiers:

Table B.2. ECMAScript Keywords

Keywords Reserved for future use
break for throw
case function try
catch if typeof
continue in var
default instanceof void
delete new undefined
do return while
else switch with
finally this  
abstract final protected
boolean float public
byte goto short
char implements static
class import super
const int synchronized
debugger interface throws
double long transient
enum native volatile
export package  
extends private  

A property of an object may have any name, even that of a reserved word. object.property and object['property'] both refer to the property property of the object named object. When using dot syntax (object.property) property names must be a valid identifiers, so object.default, for example, is a syntax error. In this case object['default'] can be used instead.

LZX script implements a subset of class declarations, as specified by the ECMAScript 4 committee wiki. This extension is used in the LZX runtime implementation, but is not yet supported in user code.