Javascript, Prototype oriented programming

this is kind of a follow-up to my Python vs C#, a REAL answer post, which in the end, breaks programming principles and how languages (mainly C# and Python) implemented them.

But then there is this strange language, Javascript (js), which is both compiled and dynamic, and mainly, imperative, and, unlike the rest, prototype oriented. whats the connection and what that means?

Imperative vs. Declerative

lets go back to that for a sec. I am writing a program, with the object Person, with name, last name, age, address, all the usual. it runs, i sell it.

after a few days, the boss comes and says the client now wants v2, with dogs...

if its a declarative language, like C#, i must declare a Dog class somewhere and declare a connection to Person, whether its by giving it a PersonId or just setting it as a property of person.

If it was Python, which has Imperative features, i could just add for a person instance a property of a simple type, like DogId. but for a Dog class i also must declare a class.

With JS i can just add a new dog object to my Person object and add properties and methods and now they all have it. anywhere. i don't need to declare the new object.


And say the boss comes and says that the Dog class now needs a method for Bark?
Same deal, C# you need to declare Bark, with a return type, and arguments, and in JS you add to the prototype function Bark. you don't even have to declare the arguments, use the arguments keyword to access them. or sometimes this.


That's the idea behind Prototype oriented programming

As widely described in wikipedia the language should cut off alllllll the declarations, create an object, spam it with your functions, and then when you feel its ready, create instances, which just link to the original proto, and if you need more copy it and add more functions, and create new instances, focus on functionality not on declaring.

And when you inherit from an object? its just has a link to its parent, each can continue with its life.

examples:


var position = {
  x : 0,
  y : 0,
  move : function(x, y){
    this.x += x;
    this.y += y;
  }
}

var animal = Object.create(position)
animal.canWalk = false,
animal.moveAnimal = function(x, y){
  if (this.canWalk){
    this.move(x,y)
  }
};

var dog = Object.create(animal);
dog.type;
dog.size;
dog.bark = function(){
  return this.type + ' - ' + this.size;
}

var rexi = Object.create(dog);
rexi.canWalk = true;
rexi.move(2, 4);

//where is rexi?
position.location = function(){
  return this.x + ' - ' + this.y;
}
rexi.location() // output "2 - 4"


1st, you can see the chain, rexi move, searches rexi object, then searches its proto dog, then proto animal, then proto position. any new move function will mask the position's move.

2nd, we can and location function to position waayyy after the inheritance is done.

Instances

quite similar, just remember each inheritance must recreate the prototype, this time



function Location(){
  this.x = 0;
  this.y = 0;
  this.move = function(x, y){
    this.x += x;
    this.y += y;
  };
}

function Animal() {
  Location.call(this);
  this.canWalk = false;
}

Animal.prototype = Object.create(Location.prototype);

function Dog(){
  Animal.call(this);
  this.canBark = true;
  this.moveDog = function(x, y){
    if (this.canWalk){
      this.move(x,y)
    }
  };
}

Dog.prototype = Object.create(Animal.prototype);

var rexi = new Dog();
rexi.canWalk = true;
rexi.moveDog(2, 4);

//where is rexi?
Location.prototype.location = function(){
  return this.x + ' - ' + this.y;
}
rexi.location() // output "2 - 4"


why here prototype and there __proto__?
__proto__ is the actual link to the above prototype, while prototype is the prototype of this object.



The main point is to remember that the 2 main cons of JS, as a Prototype oriented Language, are the prototype chaining, which is achieved by Copying object when creating, not realy creating from nothing, and the real Imperative abilities, and therefor it must be dynamic.

and how about multiple inheritance?

and, eventually, if you need a cat, since you already did all the job with dog, make it the proto for the cat :
var cat = Object.Create(Dog); //or rexi.

see this for more

enjoy.

Comments

Popular posts from this blog

c# Service Play Sound with NAudio example by Moshe

Asp.Net Ending Response options, Response.End() vs CompleteRequest()

JS/JQ simulate Enter event