Inheritance via prototypes

  • Updated

Inheritance via prototypes

The irScript language has no notion of type declaration separate from object instantiation; that is, there’s no irScript equivalent to the C# ‘class’ or ‘struct’ keywords. Rather, ScriptObject instances are created on demand and assigned properties and methods needed for them to function. This is a hallmark of many dynamic languages, and helps keep the language syntax lightweight and focused on expression of user intent as opposed to definition of extensive type hierarchies.

That’s not to say there’s no notion of hierarchy in irScript, however. irScript supports a form of object-based inheritance called prototyping. The basic idea is that during initialization, ScriptObjects can be assigned a prototype object that serves as the hierarchical parent of that object instance; all methods and properties of the prototype are callable from the child instance:

var x = { MyProperty : 22.3 }; 
var y = { Prototype : x };
var z = y.MyProperty;   // z == 22.3 var a = {};
a.Prototype = x;        // ERROR: cannot assign prototype outside of literal syntax

ScriptObject instances can “override” a prototype property or method simply by themselves

defining a member of the same name:

var x = { MyProperty : 22.3 };
var y = { Prototype : x, MyProperty : 'hello' };
var z = y.MyProperty;           // z == ‘hello’

In addition, the prototype implementation is always available via explicit invocation of the Prototype property:

var x = { MyProperty : 22.3 }; var y = { Prototype : x,
MyProperty : 'hello',
MyPrototypeProperty : this.Prototype.MyProperty
};
var z = y.MyProperty;   // z == ‘hello’
var a = y.MyPrototypeProperty; // a == 22.3

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.