简体   繁体   中英

Valid JavaScript code that is NOT valid ActionScript 3.0 code?

Most JavaScript code is also syntactically valid ActionScript 3.0 code. However, there are some exceptions which leads me to my question:

Which constructs/features in JavaScript are syntactically invalid in ActionScript 3.0? Please provide concrete examples of JavaScript code (basic JavaScript code DOM API usage) that is valid ActionScript 3.0 code. 使用DOM API的基本JavaScript代码)的具体示例,该代码有效的ActionScript 3.0代码。

You can declare a variable in JS without using the var statement. In ActionScript 3 the var statement is always required .

The following is valid JS but will throw a compiler error in AS3:

var foo = 6;
bar = "bar";

You can also redeclare a variable in a single scope JS without an error:

var x = 5;
var x;

In AS3, you can only declare a variable once for each scope.

The obvious ones are ECMAScript 4 keywords that weren't future reserved words in ECMAScript 262 3rd Edition:

// oops!
var let   = "Hello";
var yield = "World";

AS3 is a much stronger typed, and traditionally OO, language than javascript (and AS2), so all manipulation of prototypes is out. This is probably the biggest difference, IMO, since it means that something like jQuery can't really work in AS3.

As was pointed out, locals must be declared with var . Also, untyped variables and redeclared variables generate compiler warnings.

You'll generally find that there's more examples of the other way around (AS3 code not being valid in javascript).

Actionscript 1 is much closer to Javascript. Actionscript 3 follows the now defunct ECMAScript 4 spec.

For one thing, the eval() method won't work.

Also, the RegExp() constructor doesn't appear to work, at least not with strings. In other words, you can't say:

var rex:RegExp = new RegExp("[a-zA-Z0-9]+","gim");

You have to write it like this:

var rex:RegExp = new RegExp(/[a-zA-Z0-9]+/gim);

In other words, you can't do variable substitution for parts of the string argument.

Well, you can't use alert (and some other JS global functions), onmouseover, onload, etc. (JS event handlers), anything that's form-related or browser-related (as you suggest). You can't copy and paste JS code in an AS3 class because AS3 is strongly typed and you can get compiler errors (moreover, in JS you have no classes at all).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM