Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Define a JavaScript Class in ES6?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 987
    Comment on it

    Classes in ECMA 2015 is special function so like functions classes can be defined by Class Declaration and Class Expression but both have some differences as well. So, Today in this tutorial I will provide you the way to define a JavaScript Class in ES6.

     

     

    Their are 2 ways of defining class:

    1. declaration: In this way we write keyword class followed by name of the class and between curly braces constructor, Prototype methods and static methods comes. 
      • class ClassName{}
    2. expression: Here we follow the syntax to write class:
      • named:  var classname = class ClassName{} classname is local to class body
      • unnamed:  var ClassName = class {}

     

    JavaScript classes are much simpler in order to create objects and deal with inheritance. As Classes are basically exists to create Objects and hence provides data by creating methods and properties for the object.

     

    Class definition includes the following main part:

     

    1. Constructor

    Constructor is a special method of class which is used to create Object and allocate memory for it

    class ClassName {
    	construcor(param1, param2) {
    		this.param1 = param1;
    		this.param2 = param2;
    	}
    }

       Here, in the example above, we have defined the constructor within a class, so we can create object of class having any name, "ClassName" in our case and after that we can access params and methods of class by the object.

     

     

    2. Functions

    Functions/methods are meant to be use to perform relevant actions. Here we use 2 types of methods one is static and second one is prototype.

     

    Lets see both example one by one:

    • Static Method: Static keyword makes the methods to use without instantiate the class.

     

    class Student{
    	constructor(name, age){
    		this.name = name;
    		this.age = age;
    	}
    	static getData(student){
    		return student.name + has age + student.age;
    	}
    }
    student1 = new Student(Jon, 17);
    Student.getData(student1); /// Jon has age 17

            In above example, Student Class has a static method which is named getData. You can see the way of calling it outside of the class is not by instantiate it then call but direct using ClassName.staticMethodName()

     

    •  Prototype Method: Prototype methods asks you first to create instance of class and then with instance we can access these methods.        
    class Student{
    	constructor(name, age){
    		this.name = name;
    		this.age = age;
    	}
    	getData(){
    		return this.name + has age +this.age;
    	}
    } 
    student1 = new Student(Jon, 17)
    student1.getData(); /// Jon has age 17

            Here in this example, we have called the getData method by first instantiate Student class which is stored in student1 and then call it by instance.methodName()

    How to Define a JavaScript Class in ES6?

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: