The "pseudo-classical" object oriented class definition style in JavaScript seems to be dismissed by all the kewl JavaScript folk, but one of my thoughts is that JavaScript actual needs some order brought to it, for today's JavaScript mega-programs. Because there is no order, and programs become as maintainable as BASICA programs of yore. CommonJS modules help some - they can help you keep your large chunks of code better organized. But I yearn for classic OO. This is an experiment.
scooj
is designed to run in a CommonJS module system. If you need
a CommonJS module system for a browser, look at
modjewel.
scooj
is a JavaScript library that let's you define "classes" that have
instance methods and static methods. Classes can have superclasses, and
a real super method invocation is available.
See the included tests for some basic use cases.
Beyond that description, you probably won't be using the scooj
module
directly. Instead, you'll author your classes in .scoop
files, which you
compile into JavaScript source files with the scoopc.py
compiler.
If you use the scooj module functions in your code, you may not be terribly happy with the verbosity of the resultant code. It's better than doing the equivalent 'by hand', but it's still ... wordy.
scoopc is designed to fix that. It's a "compiler" which takes files consisting of JavaScript code prefixed with "directive lines", and generates new JavaScript files. The "directive lines" are lines in the file which are used to declare methods, classes, etc. See the file Animals.scoop for an example of a .scoop file.
Generally, directives are used to define functions. You specify the function/method/property name and optional parameter list in the directive, and the body of the function following the directive. The outer-most braces required by JavaScript for function definitions are not needed for the function bodies when using scoop.
One important feature of scoopc is that the resulting JavaScript file will have the same line structure as the original scoop file. If you have syntax errors in your JavaScript file, you won't have to guess at what line number the problem is back in the scoop file - it'll be the same line.
Enjoy brace-, bracket-, and comma-free class defining!
class className class className (parameter list) class className < superclassName class className (parameter list) < superclassName
The class
directive defines a new class. It can optionally define a parameter
list for the constructor, and a superclass.
The JavaScript code following this directive becomes the body of the constructor function for the class.
static method methodName static method methodName (parameter list)
The static method
directive defines a new static method on the previously
defined class.
It can optionally define a parameter list for the method.
The JavaScript code following this directive becomes the body of the static method.
static getter propertyName
The static getter
directive defines a property getter for the previously
defined class.
The JavaScript code following this directive becomes the body of the getter function for the property.
Note that this directive generates code that makes use of the ECMAScript 5 property accessor APIs.
static setter propertyName (parameter list)
The static setter
directive defines a property setter for the previously
defined class.
The JavaScript code following this directive becomes the body of the setter function for the property.
Note that this directive generates code that makes use of the ECMAScript 5 property accessor APIs.
method method (parameter list)
The method
directive defines an instance method for the previously defined
class.
It can optionally define a parameter list for the method.
The JavaScript code following this directive becomes the body of the method.
getter propertyName
The getter
directive defines a property getter for instances of the previously
defined class.
The JavaScript code following this directive becomes the body of the getter function for the property.
Note that this directive generates code that makes use of the ECMAScript 5 property accessor APIs.
setter propertyName (parameter list)
The setter
directive defines a property setter for instances of the previously
defined class.
The JavaScript code following this directive becomes the body of the setter function for the property.
Note that this directive generates code that makes use of the ECMAScript 5 property accessor APIs.
init
The JavaScript code following this directive is left unadorned in the resulting JavaScript file. It's similar to Java's static initializer blocks.
function function (parameter list)
The function
directive defines a function defined globally within the
module's scope.
It can optionally define a parameter list for the function.
The JavaScript code following this directive becomes the body of the function.
require moduleName
require moduleName as variableName
The require
directive is used to generate a require()
function within the
module. The specified module is assigned to a variable name which
is the basename of the moduleName. Optionally, you may specify the
variable name which gets used by using the as form.
The JavaScript code following this directive not otherwise processed.
requireClass moduleName
requireClass moduleName as variableName
Same as the require
directive, but getClass()
is called on the
object returned from the require()
function, which is presumably
the first class defined in the scoop module.
The command line for scoopc.py
is as follows:
scoopc.py [options] FILE FILE ...
scoopc.py
converts .scoop
files to .js
files. FILE
can be a .scoop
file or a directory
of .scoop
files. Each .scoop
file is converted to a root module, and each
directory of .scoop
files is considered a root for it's contained .scoop
files
(the directory name FILE
is not part of the module name.
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-o DIR, --out=DIR generate .js files in DIR (default: .)
-q, --quiet be quiet
-v, --verbose be noisy
Copyright (c) 2010 Patrick Mueller
Licensed under the MIT license