Prototype-based Programming - Object Construction

Object Construction

In class-based languages, a new instance is constructed through the class' constructor function, a special function that reserves a block of memory for the object's members (properties and methods) and returns a reference to that block. An optional set of constructor arguments can be passed to the function and are usually held in properties. The resulting instance will inherit all the methods and properties that were defined in the class, which acts as a kind of template from which similar typed objects can be constructed.

In prototype-based languages there are no explicit classes and objects inherit directly from other objects with whom they are linked through a property, often called prototype as in the case of Javascript. There are two methods of constructing new objects: ex nihilo ("from nothing") object creation or through cloning an existing object. The former is supported through some form of object literal, declarations where objects can be defined at runtime through special syntax such as {...} and passed directly to a variable. While most systems support a variety of cloning, ex nihilo object creation is not as prominent.

Systems that support ex nihilo object creation allow new objects to be created from scratch without cloning from an existing prototype. Such systems provide a special syntax for specifying the properties and behaviors of new objects without referencing existing objects. In many prototype languages there exists a root object, often called Object, which is set as the default prototype for all other objects created in run-time and which carries commonly needed methods such as a toString function to return a description of the object as a string. One useful aspect of ex nihilo object creation is to ensure that a new object's slot names do not have namespace conflicts with the top-level Object object. (In the Mozilla JavaScript implementation, one can do this by setting a newly constructed object's __proto__ property to null.)

Cloning refers to a process whereby a new object is constructed by copying the behavior of an existing object (its prototype). The new object then carries all the qualities of the original. From this point on, the new object can be modified. In some systems the resulting child object maintains an explicit link (via delegation or resemblance) to its prototype, and changes in the prototype cause corresponding changes to be apparent in its clone. Other systems, such as the Forth-like programming language Kevo, do not propagate change from the prototype in this fashion, and instead follow a more concatenative model where changes in cloned objects do not automatically propagate across descendants.

// Example of true prototypal inheritance style // in JavaScript. // "ex nihilo" object creation using the literal // object notation {}. var foo = {name: "foo", one: 1, two: 2}; // Another "ex nihilo" object. var bar = {three: 3}; // Gecko and Webkit JavaScript engines can directly // manipulate the internal prototype link. // For the sake of simplicity, let us pretend // that the following line works regardless of the // engine used: bar.__proto__ = foo; // foo is now the prototype of bar. // If we try to access foo's properties from bar // from now on, we'll succeed. bar.one // Resolves to 1. // The child object's properties are also accessible. bar.three // Resolves to 3. // Own properties shadow prototype properties bar.name = "bar"; foo.name; // unaffected, resolves to "foo" bar.name; // Resolves to "bar"

This example in JS 1.8.5 + ( see http://kangax.github.com/es5-compat-table/ )

var foo = {one: 1, two: 2}; // bar.] = foo var bar = Object.create( foo ); bar.three = 3; bar.one; // 1 bar.two; // 2 bar.three; // 3

Read more about this topic:  Prototype-based Programming

Famous quotes containing the words object and/or construction:

    Agreeable then to my present inclination, I formed the object of my own worship, which was no other than my own understanding.
    Sarah Fielding (1710–1768)

    There’s no art
    To find the mind’s construction in the face.
    William Shakespeare (1564–1616)