Javascript (JS) began as an add-on to Netscape, programmed in a few days by Brendan Eich in the early 1990s. Though JS is a completely different language from Java, Netscape used a similar syntax to Java and named it “Javascript”.

JavaScript is a classless, prototype-oriented language. Hence by nature, one of the language’s most powerful features is flexibility. It can get the same job done by many different ways, in many different coding styles and techniques. That feature, however, comes with the cost of unpredictability. Without a unified structure, JavaScript code can be really hard to understand, maintain and re-use.

Class-based languages usually require strong-typing, provide encapsulation, and come with standard coding convention. By generally making developers adhere to a large set of principles, written code is more likely to be predictable, extensible and scalable over time. However, they don’t have the same dynamic capability found in such language as JavaScript.

Prototype-based programming

Prototype-based programming is a style of object-oriented programming in which behaviour reuse (known as inheritance) is performed via a process of cloning existing objects that serve as prototypes. Javascript is one of the only [mainstream] object-oriented languages to use prototypal inheritance. Almost all other object-oriented languages are classical.

In classical inheritance, the programmer writes a class, which defines an object. Classes can then be organized into a hierarchy, furthering code reuse. More general code is stored in a higher-level class, from which lower level classes inherit. This means that an object is sharing code with other objects of the same class, as well as with its parent classes.

In the prototypal inheritance form, objects inherit directly from other objects. All of the business about classes goes away. If you want an object, you just write an object. But code reuse is still a valuable thing, so objects are allowed to be linked together in a hierarchy. In javascript, every object has a secret link to the object which created it, forming a chain. When an object is asked for a property that it does not have, its parent object is asked… continually up the chain until the property is found or until the root object is reached.

Each object in javascript actually has a member called “prototype”, which is responsible for providing values when an object is asked for them. Adding a property to the prototype of an object will make it available to that object, as well as to all of the objects which inherit from it.

Prototype-based OO is very lightweight and flexible, offering a very high power-to-weight ratio, Multiple inheritance is easy – you just copy a subset of the properties from one prototype and paste them over the properties in another prototype, It’s quite easy to change behaviour at runtime – i.e. you can change an properties and “morph” an object pretty much arbitrarily at runtime. Allows for cool in-game effects, and if you couple this with a “scripting language” then pretty much anything is possible at runtime.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: