Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class use in a prototypal inheritance-based language

The following answer to this question does a great job explaining the differences between classical inheritance and prototypal inheritance. this was of interest to me to understand because I started working in Java, but moved over to Javascript.

In his answer, he states for prototypal inheritance that, "All of the business about classes goes away. If you want an object, you just write an object."

Yet there is so much documentation and questions on how to "write classes" in Javascript.

Why is there push to make the language something it is not. I'm looking for concrete examples of cases where using classes in JS applications are more sensible in this prototypal language and the benefits of awkwardly fitting a square peg into a round hole. As Aravind put it, why are people learning Javascript by comparing it to others, and not as it was intended... and why is this practice seemingly encouraged?

Bottomline question: Why are classes being introduced in ECMAScript 6?

like image 693
user3871 Avatar asked Nov 21 '14 20:11

user3871


People also ask

What languages use prototypal inheritance?

Prototypal inheritance is a form of object-oriented code reuse. 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.

Which of the following is a feature of prototypal inheritance?

Prototypical inheritance allows us to reuse the properties or methods from one JavaScript object to another through a reference pointer function. All JavaScript objects inherit properties and methods from a prototype: Date objects inherit from Date.

What is prototypal inheritance give example?

Here we can say that " animal is the prototype of rabbit " or " rabbit prototypically inherits from animal ". So if animal has a lot of useful properties and methods, then they become automatically available in rabbit . Such properties are called “inherited”.

What is the difference between class and prototypal inheritance?

The most important difference between class- and prototype-based inheritance is that a class defines a type which can be instantiated at runtime, whereas a prototype is itself an object instance.


1 Answers

The masses like classes.

There's nothing "more" or "less" natural about prototypal inheritance, this is entirely subjective. JS is its own language, just like Smalltalk and Self had different ideas about what it meant to be an object.

ES6 classes are syntactic sugar. They normalize/clean up how inheritance/etc are to be used in JS.

Similar to CoffeeScript, they attempt to standardize how OOP is done in JS, and make it more familiar to people that aren't used to prototypal inheritance.

like image 95
Dave Newton Avatar answered Oct 23 '22 14:10

Dave Newton