CLASSES IN JAVASCRIPT

CLASS DEFINITION

CLASS DEFINITION In object-oriented programming, a class is a construct of a programming language used as a model for creating objects. The model  includes attributes and methods that will be shared by all objects created (instances) from the class. An "object" is, in fact, the instance of a class. In modern JavaScript there is a more advanced "class" construct, which introduces very useful new possibilities for object-oriented programming. THE SYNTAX OF CLASS class MyClass {   // metodi della classe   constructor() { ... }   method1() { ... }   method2() { ... }   method3() { ... [...]

By |2024-11-11T18:34:52+00:00April 22, 2022|0 Comments

CLASSES IN THE JAVASCRIPT LANGUAGE

DEFINE A CLASS AND ITS CONSTRUCTOR A constructor function normally receives parameters as input. The keyword this makes sure that the objects created by the constructor function have a copy of those properties, properties having as values the values passed in as input. Let us see with a code example the differences between a constructor function and a class. Creating an object from a constructor function or creating an instance from a class with the same properties gives the same results. The difference is that the properties of a class must be initialized in a special [...]

By |2024-11-11T18:35:25+00:00November 21, 2022|0 Comments

INHERITANCE IN JAVASCRIPT

Inheritance in javascript is a feature that allows a class to extend the properties of other classes. THE KEYWORD EXTENDS Let's assume we have an Animal class: class Animal {   constructor(name) {     this.speed = 0;     this.name = name;   }   run(speed) {     this.speed = speed;     alert(`${this.name} runs with speed ${this.speed}.`);   }   stop() {     this.speed = 0;     alert(`${this.name} stands still.`);   } } let animal = new Animal("My animal"); Here we see how to represent the Animal object and the Animal class graphically: We may want to [...]

By |2024-11-11T18:36:17+00:00April 24, 2022|0 Comments

STATIC METHODS

STATIC METHODS We can also assign methods to the classes themselves, not just their "prototype". These methods are called static. Inside the class, these are preceded by the keyword static, as we can see in the example: class User {   static staticMethod() {     alert(this === User);   } } User.staticMethod(); // true The value of this in the User.staticMethod() call is represented by the constructor of the User class (the rule of the object before the dot). Usually, static methods are used to represent functions that belong to the class, but not to a particular object. For example, we may have objects of type Article and need a [...]

By |2024-11-11T18:37:06+00:00April 25, 2022|0 Comments

ENCAPSULATION IN JAVASCRIPT

JAVASCRIPT ENCAPSULATION One of the most important concepts of object-oriented programming is encapsulation, or the delimitation of internal interfaces from the external ones. This practice is a "must" in the development of any application that is more complex than "hello world". To understand it, let's step out of the development world and look to the real world. Usually, the devices we use are quite complex. Being able to delimit their internal interface from the external one allows us to use them without major problems. AN EXAMPLE FROM THE REAL WORLD Let's take the example of a coffee machine. Simple [...]

By |2024-11-11T18:37:43+00:00April 26, 2022|0 Comments
Go to Top