A Prototype-based Object Oriented Scripting

Prototype Creation

prototype ( { prototype } )

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.
foo = prototype()
bar = prototype(foo)

Attribute Definition

prototype . name = value

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 {, args..} ) block

To define a method, just define an attribute as a function that takes the additional parameter, 'this'.

e.g.
foo = prototype()
foo.name = "Foo"
foo.hello = function (this) println("hello ", this.name)
bar.prototype = foo.clone()
bar.name = "Bar"
bar.hello()

A Sample Code

use("metaobject")

function stack(size){
  p = prototype()
  p.array = Object[size]
  p.pos = 0
  p.push = function(this, obj) this.array[this.pos++] = obj
  p.pop = function (this) this.array[--this.pos]
}

s = stack(10)
s.push("foo")
..
s.peek = function (this) this.array[this.pos - 1]
s.peek()