Classes and Object-Oriented Programming in Java Script

 Classes and Object-Oriented Programming in Java Script


JavaScript also supports object-oriented programming through the use of classes. A class is a blueprint for creating objects with a specific set of properties and methods.


Here's an example of a class that represents a person:


kotlin


class Person {

  constructor(name, age,

occupation) {

this.name = name;

this.age = age;

this.occupation = occupation;

}


greet() {

console.log("Hello, my name is " + this.name + " and I'm a " + this.occupation + ".");

}

}


To create a new instance of this class, you would use the new keyword:


javascript


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

john.greet(); // prints "Hello, my name is John and I'm a Software Developer."

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