views:

376

answers:

6

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 without DOM API usage) that is NOT valid ActionScript 3.0 code.

+2  A: 

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

spender
+4  A: 

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";
Andy E
Why the down-vote? +1 from me!
knorv
@knorv: I foolishly complained about the downvotes everyone received on meta.stackoverflow.com. The people there obviously thought it would be humerous to downvote my answer even more.
Andy E
Andy E: OK, that explains it. Hope your answer will be upvoted since it is a good answer.
knorv
+1 from me, good answer
c0mrade
+1  A: 

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.

Robusto
The docs suggest that you can use strings for arguments to the `RegExp` constructor, it doesn't make sense not to - there'd be no benefit using the constructor if you're just passing a literal anyway. Link: http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/RegExp.html
Andy E
@AndyE: Yeah, I know it doesn't make sense. I'm just reporting that it did not work for me, and quite recently.
Robusto
Both versions of the code posted compile and work fine in Flash CS4.
fenomas
@Fenomas: I use FlexBuilder 3 and it gives me errors. So you downrank my answer for that?
Robusto
@Robusto - both lines compile fine for me under SDK 3.1
Richard Szalay
@Richard Szalay: I'm using 3.3, but that shouldn't matter. It's not that it throws a compile error, it just doesn't work for me. It doesn't *find* anything, whereas the non-string version does.
Robusto
+1  A: 

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

+6  A: 

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.

Reuben
+3  A: 

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

Richard Szalay