Brian Genisio
Brian Genisio

Reputation: 48137

Can you take a Javascript library and run it in Flash?

Lets say I pick up a pure Javascript library... one that doesn't rely on the DOM in any way.

Now, lets say I want to take that library and us it in Flex. Is there any good way to do this? I can't simply include the Javascript, as it is not close enough to ActionScript to compile... but is there a good converter that will do it for me? Take the Javascript prototype style classes and convert it to ActionScript classes?

Lastly, I don't want to use the Browser for this. I know that I can always use ExternalInterface to access the browser, but I am not always running my Flex app in the browser... sometimes it is Air, and sometimes it is in a PDF.

Any thoughts on this?

Upvotes: 2

Views: 625

Answers (3)

martineno
martineno

Reputation: 2635

Depending on the complexity of the library you want to use it could be either trivial modification by adding a few required parameters (such as the package {} wrapper and so on). However, some of the transformations wouldn't be trivial and might require a full on compiler.

I do wonder how much work it would be to write a new backend for the Closure Compiler. Haven't looked into the source code of it, but I assume it is reasonably modular. That way the conversion would be automatic modulo the things that you can't do in AS3 such as DOM manipulation.

Even using this though you wouldn't get some of the nice things of AS3 such as compile time type checks. Also prototype to inheritance conversion is a non-trivial transformation, so you probably wouldn't get that either.

Upvotes: 1

James Ward
James Ward

Reputation: 29433

Usually porting from JS to AS isn't too hard. But you might want to check out Jangaroo. It goes the other direction, AS -> JS but might be able to be used in reverse.

Upvotes: 3

Robusto
Robusto

Reputation: 31883

Why even bother? ActionScript already has util classes for things you might need to do, and most Javascript libraries are concerned with getting the DOM to behave. One of the nicest features of jQuery, for example, is the ability to address any element by its CSS selector. And you will never get that in ActionScript because it simply wouldn't make sense.

Maybe a better question is, what features of a Javascript library would you wish to see in ActionScript? Very likely there's already a class that does what you want, and if there isn't it's easy to create one.

Upvotes: 3

Related Questions