extending prototype objects in Rebol
In JavaScript and other prototype-based languages it is very natural to extend an object.
In Rebol it is only slightly less obvious.
Much like using the object-literal notation or JSON in JavaScript, it is not necessary to use an explicit reference to Object. An object in Rebol is much more like a record in Oz which can reference functions as first-class objects.
But if we do want an object as a prototype from which to construct other objects, then we use
my-obj: make object! [answer: 42 question: "unknown"]
When it comes time to extend my-obj there are at least two options:
my-obj: make my-obj [my-extension: "worm-hole"]
We can see the new attribute by using probe
probe my-obj
The alternative would have been
my-obj: make my-obj append [my-extension2: "black-hole"] [] ; note the empty block
To use my-obj as a protoytpe is to use make
my-derived-obj: make my-obj [another-extension: "not derived"]
probe my-derived-obj
In Rebol3 we may have the option to simply extend a prototype as we now do in JavaScript or Io
Leave a Reply