What to use instead of classes in JavaScript?

Background

I am a developer with a background of about 9 years in both Java and C #.

In these languages, classes and taxonomic classification are both inherent in the object paradigm.

So, when ECMA6 came out , I was very happy to see classes in this language ... and I started using them everywhere,

Problem

It turns out that using classes in JavaScript is a trap.

If you use them, you will go to your grave, not knowing how unhappy you are.

And you will never understand JavaScript. You will think yes, but you will not.

Questions

So clearly, after watching this full conference, I realized that I donโ€™t know JavaScript .

All my life I have been formatted using OOP with a class paradigm, and now I donโ€™t even know where to look for help or even start.

  • In JavaScript style, how would you imagine the animal kingdom with inheritance? I would use the Animal class and then the Cool Dog and, for example, an instance of dog objects. This is not a JavaScript method.

Non-JavaScvript example:

class Animal{ constructor(aName){ this.name = aName; } } class Dog extends Animal{ constructor(){ super(aName); this.type = "Furry dog"; } } let myDog = new Dog("Boby"); 
  1. What is the way to execute javascript?

At this moment I am looking for guidance. After trying, I could not find anything useful, mainly because I believe that I was so lost that I did not even look for the right things.

Thanks in advance.

+7
javascript ecmascript-6
source share
1 answer

As far as I know, JavaScript is a prototype-based language with first-class functions, as you can read here .

Now. This is actually just an OPP style, meaning you will work and think about objects and inheritance. You just need to know that there are no classes in JavaScript except prototypes. But remember. All about features.

Object Constructors

To create your own object definition, you must do the following:

 function Animal() { this.name = null; this.age = 0; this.genus = null; this.isWild = true; }; Animal.prototype.hasName = function() { return this.name !== null; }; Animal.prototype.isItWild = function() { return this.isWild; }; // Here we create an instance of Animal var someAnimal = new Animal(); // Let give it a name someAnimal.name = 'Dobby'; console.log('Hey there. My pet name is %s', someAnimal.name); console.log('is it wild? %o', someAnimal.isWild); 

You now have a prototype of Animal. See how to extend your properties to create a Dog prototype:

 function Dog() { this.genus = 'Canis'; this.race = 'unknown'; } Dog.prototype = Object.create(Animal.prototype); Dog.prototype.constructor = Dog; Dog.prototype.bark = function() { console.log('rrouff!); }; 

Now create a dog race:

 function SiberianHusky(name, age) { this.name = name; this.age = age; this.race = 'Siberian Husky'; } SiberianHusky.prototype = Object.create(Dog.prototype); SiberianHusky.prototype.constructor = SiberianHusky; // Let create our Siberian Husky var myPet = new SiberianHusky('Bobby', 5); // Aww. It is barking! myPet.bark(); 

Prototype object

The examples show that each definition uses a prototype of the object . This is used to determine how an object is. You can consider this as a class definition.

Avoiding overuse of the prototype property

Instead of writing a prototype each time, you can do the following:

 Animal.prototype = { name: null, age: 0, genus: null, isWild: true, hasName: function() { ... } isItWild: function() { ... } } 

To fully understand the concept of prototypes and how they are inherited from each other, you can check this .

Latest notes

JavaScript is a language with several paradigms. You can use the Object Oriented Programming Paradigm, the Intelligent Programming Paradigm, and the Functional Programming Paradigm , which means you can program the same application in different ways.

Some useful links

https://en.wikipedia.org/wiki/JavaScript

https://en.wikipedia.org/wiki/Prototype-based_programming

Greetings.

+8
source share

All Articles