Object-Oriented Programming (OOP)in JavaScript

  Object-Oriented Programming (OOP)in JavaScript


Object-Oriented Programming (OOP) is a programming paradigm that is widely used in JavaScript. It provides a way to structure code using objects and classes, which can be used to encapsulate data and behavior.


Here's an example of how to define a class in JavaScript:


javascript


class Person {

  constructor(name, age) {

    this.name = name;

    this.age = age;

  }


  sayHello() {

    console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old.");

  }

}


var john = new Person("John", 30);

john.sayHello(); // prints "Hello, my name is John and I am 30 years old."

The Person class defines a constructor method that sets the object's properties, as well as a sayHello method that prints a message to the console.

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