What are the advantages of a classic structure over a prototype?

I just recently started programming significantly, and being completely self-taught, I, unfortunately, do not have the benefits of a detailed computer science course. I have read a lot about JavaScript lately and I'm trying to find an advantage in classes over JavaScript prototype character. It seems that the question is outlined in the middle, which is better, and I want to see its classic side.

When I look at a prototype example:

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

And then the classic 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 started thinking about it because I am watching the new edition 5, and many people see that they have not added a class system.

+4
source share
5 answers

Inheritance is really impossible without using a pseudo-classic style. See what Douglas Crockford has to say about this. Even if you used a purely prototype object structure, you would need to create a constructor function for inheritance (which Crockford does, then abstract it using the create method, and then call it a pure prototype)

+3
source

When you create an object through new , it will use the prototype chain to search for properties not found in the instance.

So, for example, you can add the add method to A.prototype only once, instead of overriding a new copy of the same function every time you create a new instance of A

 function A(x,y){ this.X = x; this.Y = y; }; //define add once, instead of every time we create a new 'A' A.prototype.add = function(){ return this.X+this.Y; }; 
+3
source

In the second method, you can instantiate A , letting you have several at a time. For instance:

 var inst_one = new A(123,321); var inst_two = new A(456,654); // some meaningful code here... document.write(inst_one.add()); // some more meaningful code here... document.write(inst_two.add()); 

The example you provided is trivial, so let's make a more intuitive example:

 function Comment(user,text){ this.user = user; this.text = text; this.toString = function(){ return '<span class="comment">'+this.text+' - <a class="user">'+this.user+'</a></span>'; }; }; var comments = someFunctionThatReturnsALotOfCommentObjects(); for(c=0;c<comments.length;c++) document.getElementById('comments_container').innerHTML += comments[c].toString(); 

This (hopefully) demonstrates the advantage of being able to create multiple instances of a class. This is not a fundamental concept of JavaScript, it is a fundamental concept of Object Oriented Programming , which may be why you are not familiar with it if you do not have formal programming courses.

+2
source

To be fair, JavaScript has classes. Well, maybe some argue with that, but I think it's just semantic hair splitting. If the class is considered a blue font for creating objects, then the JavaScript function can serve as that. There is not much difference in function, only the form:

 class someObj { private int x; public int returnX() { return x; } } someObj temp = new someObj(); 

-

 function someObj { var x; this.returnX = function() { return x; }; } var temp = new someObj(); 

They are different under the hood, but you can use any form to serve the same end.

Prototype really differs in inheritance. In prototypical inheritance, when you create a new object, you really copy an instance of the prototype object, and then add new fields or members to it. On the other hand, classical inheritance does not deal with an instance, but simply a β€œblue print”. For example, in JavaScript, you would say:

 temp.prototype = new someOtherObj(); //Creating a new instance to serve as the prototype. 

In classic language you can say:

 class someObj : someOtherObj //Copying the "blue print" over. 

The implication is that data will be transferred between derived objects in the prototype language. I once wrote a function and got another function from it using prototyping in JavaScript. This base object contained a reference to the DOM object, and when I changed it in one child object, it would change it for all instances of this child object. This is because, again, in the prototype language, you are creating an instance, not a β€œblue print”.

+1
source

The classic version allows you to declare private variables with a scope inside the object, while the prototype version does not work.

0
source

Source: https://habr.com/ru/post/1310525/


All Articles