What is a prototype based language in terms of javascript

Today i was going through the basics and since switching from a class based language to a prototype based language can be a a little tricky , i will leave this post here.

Now. What the wiki says :

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. This model can also be known as prototypalprototype-oriented, classless, or instance-based programming. Delegation is the language feature that supports prototype-based programming.


So , what we can understand that in a prototype based language , objects are created by cloning existing objects(prototypes)  and adding your own properties to it .Actually, in javascript, when  we are creating an object, we are always creating a clone of an existing object .
var o= {a:1,b:2};

Now, this object inherits from a base object in javascript called Object.prototype which has its own methods and properties . Infact , this object  also inherits from the topmost base  object - 'null' , yes , null ( i am not sure if i should call this an object or not , but i like to think it this way for consistency in the model ) .

So, if you give console.log(o.hasOwnProperty) , you will see a function definition in the console :
function hasOwnProperty() { [native code] }


which means that you have inherited properties from Object.prototype and hasOwnProperty is one of its methods .

 So , basically :
0---> Object.prototype -----> null 


Similarly Arrays inherit from Array.prototype and functions inherit from function.prototype which have their own methods and proprties  and both of these inherit from null . 

So , what if we want to create an object from scratch without inheriting from anything. 
Yes, you guessed it right we will inherit from null and give our own properties to it . 

var cleanObject=Object.create(null);

cleanObject.pi=3.14;

So you can see cloning(inheritance) is going on everywhere . And unlike class based language , there is no separate object definition and instance ,i.e, usually in class based languages, object definition does not occupy memory but in javascript they too occupy memory . 
Suppose you  create a function , and it is both an object definition and instance . like :
function hello(){
console.log('Hello World');
}
hello() // working as an instance 
var hi = new hello() // working as an object definition


Which means that there is no explicit class , objects inherit directly from other objects through properties, called prototypes in javascript . So if you define and attach anything to the prototype property of an object , then it will be visible to all the objects inheriting from that object . And if you change that definition later in any object , then it will be visible to all objects . like in this example :

var Person = function() {

  this.canTalk = true;

};

Person.prototype.greet = function() {

  if (this.canTalk) {

    console.log('Hi, I am ' + this.name);

  }

};

var Employee = function(name, title) {

  Person.call(this);

  this.name = name;

  this.title = title;

};

Employee.prototype = Object.create(Person.prototype);

Employee.prototype.constructor = Person;

var bob = new Employee('Bob', 'Builder');

var rg = new Employee('Red Green', 'Handyman');

bob.greet();

rg.greet();

Person.prototype.greet=function(){

  console.log('Just for testing');

}

bob.greet();

rg.greet();

So in the above example, we change the greet function, and the change is reflected across all instaces .

And yaa about that last line 'Delegation is the language feature that supports prototype-based programming.' , if you guys are wondering what delegation is , then to sum it up , it is similar to classical inheritance but with much lighter memory footprint invloved and entangles the overly complex hierarchies . Means, you can call any function in context of an instance of another function without considering the hierarchies involved . It simplies inheritance actually . Delegation is primarily achieved in javascript by 'call' and 'apply' .You can use 'call' and 'apply' to call any function inside an instance of a function . 
If you want to learn more about delegation , then you can follow this article . 
So thats all you need to get the feel of a prototype based language . 
Cheers

0 comments:

Post a Comment