1 min read

JavaScript Classes

Class methods are created with the same syntax as object methods.Use the keyword class to create a class.Always add a constructor() method.
JavaScript Classes

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

class ClassName {
 constructor() { ... }
}

class Temp { constructor(name, year){
this.name = name;
this.year = year;
 }}

Note: A JavaScript class is not an object.It is a template for JavaScript objects.

When you have a class, you can use the class to create objects.

let temp1 = new Temp("x", 2014);
let temp2 = new Temp("y", 2019);

Constructor

The constructor method is a special method:

  • It has to have the exact name "constructor"
  • It is executed automatically when a new object is created
  • It is used to initialize object properties

Note: If you do not define a constructor method, JavaScript will add an empty constructor method.

http://learnwebtutorials.com/wp-content/uploads/2015/08/es6-classes.jpg
JavaScript Classes
https://www.w3schools.com/js/js_classes.asp

#JavaScript #ES6 #Class #Constructor #Probyto #ProbytoAI

Subscribe and follow us for latest news in Data Science and Machine learning and stay updated!

Facebook: https://facebook.com/probyto
Twitter: https://twitter.com/probyto
LinkedIn: https://linkedin.com/company/probyto
Instagram: https://instagram.com/probyto