Posts

JavaScript Class

  ECMAScript 2015, also known as ES6, introduced JavaScript Classes. JavaScript Classes are templates for JavaScript Objects. JavaScript Class Syntax Use the keyword  class  to create a class. Always add a method named  constructor() : A JavaScript class is  not  an object. It is a  template  for JavaScript objects. The constructor method is called automatically when a new object is created. The basic syntax is: class MyClass { // class methods constructor ( ) { ... } method1 ( ) { ... } method2 ( ) { ... } method3 ( ) { ... } ... } Then use  new MyClass()  to create a new object with all the listed methods. The  constructor()  method is called automatically by  new , so we can initialize the object there. For example: class User { constructor ( name ) { this . name = name ; } sayHi ( ) { alert ( this . name ) ; } } // Usage: let user = new User ( "John"...