ExtendScript / JavaScript implementation - Classes???

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Darkmoon_UK
Posts: 62
Joined: September 5th, 2006, 3:45 am
Location: Chiswick, London, UK
Contact:

Being a Java coder at heart, I really want to be able to use classes, constructors etc. in my scripts. I don't mind if I do this by using JavaScript 2.0 features e.g.

http://www.mozilla.org/js/language/js20 ... asses.html

Or by something like the importPackage()and importClass() functions, that are supposedly in ECMAScript, as suggested here:

http://osdir.com/ml/text.xml.batik.user ... 00053.html

However, neither of these work! I have used the ability to emulate some OOP functionality just treating function() as a class definition and using 'this.' for instance variables... but, man, isn't there a better way to do it?? After coming from a strongly typed coding environment, I lose track of things doing it that way, and it just feels plain nasty.

If there are any decent solutions to this, I'd be very grateful to hear them.

Thanks,

Chris
Mylenium
Posts: 139
Joined: July 20th, 2005, 12:07 am

Darkmoon_UK wrote:Being a Java coder at heart, I really want to be able to use classes, constructors etc. in my scripts. I don't mind if I do this by using JavaScript 2.0 features e.g.

http://www.mozilla.org/js/language/js20 ... asses.html

Or by something like the importPackage()and importClass() functions, that are supposedly in ECMAScript, as suggested here:

http://osdir.com/ml/text.xml.batik.user ... 00053.html

However, neither of these work! I have used the ability to emulate some OOP functionality just treating function() as a class definition and using 'this.' for instance variables... but, man, isn't there a better way to do it?? After coming from a strongly typed coding environment, I lose track of things doing it that way, and it just feels plain nasty.

If there are any decent solutions to this, I'd be very grateful to hear them.

Thanks,

Chris
No. Custom classes and packages are not supported and it's stated in the docs somewhere. Your only way to halfway structure larger projects is to cross-reference different scripts (which is possible without problems) and have one of them as a container for functions, one for the UI only etc.

Mylenium
[Pour Mylène, ange sur terre]
vidpat
Posts: 86
Joined: October 21st, 2004, 12:36 am
Location: Phoenix, AZ
Contact:

JavaScript is an interesting language in this regard. I haven't seen any 2.0 implementations of the language in the wild. Yes, it sometimes feels "nasty" to use dynamic typing, but it also has its advantages. I find myself using some sort of Hungarian notation to make up for the lack of typing. JavaScript is OO, but prototype-based, not class-based. Thus, you can get some very nice OO-style behavior by thinking a bit differently. (Note: I haven't done this style of JavaScript in my current AE scripts, but would in the future. I have used OO-style JavaScript for other projects, typically AJAX applications.)

There are several ways to accomplish this, all based on the fact that functions are first-class objects. It sounds like you are starting to do this by qualifying instance variables with 'this'. Perhaps you've seen some more of what follows.

Here is a summary of the way I work:

Define a function for the class that I want to create. This function will serve as the object itself and contains effectively the declaration of private instance variables (declared and referenced with 'this') as well as the contents of the constructor.

Code: Select all

function CustomObject(iParam1, iParam2) {
    this.instanceVariable = param1;
    // constructor implementation
}
Static members are scoped by attaching them to the function object.

Code: Select all

CustomObject.STATIC_CONSTANT = 42;
CustomObject.staticFunction = function (p) {
    // static function implmentation
    // access only to static members
};
The rest of the "class" implementation is fleshed out by assigning an object literal to the class-object's prototype property. Using the object literal notation looks a bit more like defining a class.

Code: Select all

CustomObject.prototype = {

    aMemberFunction: function (sName, iCount) {
        // do something
    },

    anotherMember: function () {
        // do other things
    }
};
Now, you can create and use an instance of the class as such:

Code: Select all

var anInstance = new CustomObject(2603, 1);
anInstance.aMemberFunction("a lerb mishta", CustomObject.STATIC_CONSTANT);
Inheritance gets into more detailed concepts such as prototype chaining... but it is possible. Also, it is almost trivial to alter prototypes and instance members after they've been created and instantiated. Unlike classes, modifying a prototype does not affect existing instances of it.
bradshaw1965
Posts: 98
Joined: March 14th, 2006, 2:16 pm
Location: Atlanta, GA
Contact:

I'm still forming my opinion on strong and static typing, but I really find it interesting how in two environments I follow, folks moving from dynamic typing in an ECMA environment like Actionscript 1-2 to a more strongly typed Actionscript 3 are really excited about it and in Mac development C, C++, and Objective-C folks who've had strong and sometimes static typing are really excited about dynamic languages like Python and Ruby (although Objective-C is pretty dynamic).

I guess you always want what you don't have?
Dale Bradshaw
Technology Director | Primal Screen
creative-workflow-hacks.com
Atlanta, GA
Post Reply