In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).
– Wikipedia
The basic syntax is:
class MyClass {
// class methods
method1() { ... }
method2() { ... }
method3() { ... }
...
}
const myClassObj = new MyClass();
In ES7 you don’t need to use Constructor
or this
keyword. Also you can use the arrow functions to declare the methods.
Example for class
class Speaker {
speakerName = "Philips";
printSpeakerName = () => {
console.log(this.speakerName);
}
}
const speakerObj = new Speaker();
speakerObj.printSpeakerName();
Syntax for ES6
You have to use the constructor
and this
keyword in ES.
class MyClass {
// class methods
constructor() { ... }
method1() { ... }
method2() { ... }
method3() { ... }
...
}
const myClassObj = new MyClass();
Example for class in ES6
class Speaker {
constructor(){
this.speakerName = "Philips";
}
printSpeakerName(){
console.log(this.speakerName);
}
}
const speakerObj = new Speaker();
speakerObj.printSpeakerName();
Use the share button below if you liked it.
It makes me smile, when I see it.