Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the benefits of a classical structure over a prototyple one?

I have only recently started programming significantly, and being completely self-taught, I unfortunately don't have the benefits of a detailed Computer science course. I've been reading a lot about JavaScript lately, and I'm trying to find the benefit in classes over the prototype nature of JavaScript. The question seems to be drawn down the middle of which one is better, and I want to see the classical side of it.

When I look at the prototype example:

var inst_a = {
  "X": 123,
  "Y": 321,
  add: function () {
    return this.X+this.Y;
  }
};
document.write(inst_a.add());

And then the classical version

function A(x,y){
  this.X = x;
  this.Y = y;
  this.add = function(){
    return this.X+this.Y;
  };
};
var inst_a = new A(123,321);
document.write(inst_a.add());

I begun thinking about this because I'm looking at the new ecmascript revision 5 and a lot of people seem up in arms that they didn't add a Class system.

like image 958
Rixius Avatar asked Jan 23 '23 01:01

Rixius


1 Answers

Inheritance is not really possible without using the psuedoclassical style. See what Douglas Crockford has to say about it. Even if you were to use a purely prototypal object structure, you'd have to create a constructor function for inheritance (which Crockford does, then abstracts it behind a create method then calls it purely prototypal)

like image 140
Hooray Im Helping Avatar answered Feb 06 '23 14:02

Hooray Im Helping