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; };
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;
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.