A Prototype-based Object Oriented Scripting
pnuts.ext.Prototype class is plug-in module for prototype-based OO scripting.
The source code is provided
in the distribution package.
Sample scripts are also in the distribution package.
Prototype creation
The pnuts.ext.Prototype class is the only class needed for prototype-based
OO scripting. In prototype-based system both attributes
and behaviors are in Prototype objects, although, in
class-based system, attributes are in instances and behaviors are in the class definition.
e.g.
Attribute definition
Attributes need not to be declared.
e.g.
foo = Prototype()
foo.age = 20
|
Clone a prototype
- prototype . clone() or
- prototype ()
|
e.g.
foo = Prototype()
foo.age = 20
foo2 = foo.clone()
foo2.age ==> 20
|
Prototype chain
- prototype1 . prototype = prototype2
- prototype1 . prototype
|
"prototype" is a special attribute to chain Prototype objects.
When an attribute is refered, it is searched in the Prototype object
first. Then in a chain of the object, and so on.
e.g.
foo = Prototype()
foo.age = 20
bar = Prototype()
bar.prototype = foo.clone()
bar.age ==> 20
|
Method definition
prototype . methodName =
function (this){
function _ ( arg ) block
...
}
|
To define a method, just define an attribute which has a name of the method.
e.g.
foo.hello = function (this) function _() println("hello world")
bar.hello()
|
Usually outer functions are anonymous.
Anonymous function is useful because every anonymous function has a unique identifier.
The name of the parameter can be any variable name. But the name "this" is
recommended because it is descriptive.
And underscore("_") is a recommended name for inner functions.
When methods are overloaded, inner functions must have a same name.
Note that overloaded methods should be defined at a time.