Appendix B. ECMAScript

Table of Contents

1. Including Script
2. Differences between ECMA-262 and LZX
2.1. Exceptions
2.2. Accessing Object Properties
2.3. Browser Objects
2.4. ECMAScript object support
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>
      <script src="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 SWF8 runtime.

2.1. Exceptions

There are no exceptions, and no exception handling in the SWF8 runtime: the SWF8 runtime does not signal any runtime errors.

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 SWF8 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 51, Debugging) and to write unit tests (Chapter 52, Unit Testing).

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

2.2. Accessing Object Properties

In JavaScript, you can access the properties of an object in two ways:

foo.length  
foo['length'] 

In Javascript, these two expressions have identical meaning. The second form is normally used if you are trying to access a property whose name is stored in a variable. For instance, to enumerate the properties of an object you can say:

for (var key in myobject) ... myobject[key] ...

But in LZX script, there is one other subtle difference. When you use . to access a property, if the property does not exist, you will get a warning. If you use [ to access that non-existent property, you don't. In either case, the value of the expression will be undefined.

2.3. 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.

2.4. ECMAScript object support

LZX does not support the ECMAScript objects, attributes and methods presented in this font in the following table in the SWF8 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, ...)

toLocaleString()

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() [b]

Global

Infinity

NaN

undefined

decodeURI()

decodeURIComponent()

encodeURI(uri)

encodeURIComponent(component)

escape(string)

eval(string) [c]

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(...) [d]

min(...) [d]

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()

toLocaleString()

toFixed()

toExponential()

toPrecision()

valueOf()

Object

Object.prototype

constructor

Object()

Object(value)

new Object()

hasOwnProperty(v)

isPrototypeOf(v)

propertyIsEnumerable(v)

toLocaleString()

toString() [e]

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) [f]

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] Function.toString always returns "[type Function]".

[c] 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.

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

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

[f] 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 proposed by the ES-Harmony committee wiki. This extension is used in the LZX runtime implementation, but is not yet supported in user code.