Object-oriented programming with classes

 Object-oriented programming with classes


JavaScript has always had support for object-oriented programming, but it used to be done using prototypes and constructor functions. The class syntax is a newer way to write object-oriented code that is more similar to other programming languages like Java or C++.

Here's an example of using classes to create a simple object with a method:



class Person {

  constructor(name) {

    this.name = name;

  }


  sayHello() {

    console.log(`Hello, my name is ${this.name}.`);

  }

}


const person = new Person('Alice');

person.sayHello();

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...