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.
foo = prototype() bar = prototype(foo)
Attributes need not to be declared.
foo = prototype() foo.age = 20
foo = prototype() foo.age = 20 foo2 = foo.clone() foo2.age ==> 20
"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.
foo = prototype() foo.age = 20 bar = prototype() bar.prototype = foo.clone() bar.age ==> 20
To define a method, just define an attribute as a function that takes the additional parameter, 'this'.
foo = prototype()
foo.name = "Foo"
foo.hello = function (this) println("hello ", this.name)
bar.prototype = foo.clone()
bar.name = "Bar"
bar.hello()
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()