samedi 2 septembre 2017

Why JavaScript objects doesn't gets copy of data and methods in ES6

One common aspect of object-oriented design (in most of the languages, especially Java) is that when we construct new object using constructor, all the methods and public/protected instance variables gets copied into the object. For example:

Car myCar = new Car('Corolla', '2015');

Now if Car class has goForward() or goBackward() methods, all of them will get copied to myCar object.

But this is not the case in JavaScript as long as I've studied. If this has been similar in JavaScript, myCarhad prototype that actually points to the Car prototype.

According to my understanding, this copying of data and methods is the ultimate requirement if we want to use class-inheritance as a design pattern. Is JavaScript really lacking this feature or just my understanding is vague? Does the new ES6 class-based structure brings this thing into picture?

Thanks for clarifying my concepts, I'm very confused over this as I've been doing things in Java in the past.

Aucun commentaire:

Enregistrer un commentaire