ES6 Tutorial

JavaScript ES6 Tutorial

 

What is ES6?


ES6 known as ECMAScript6, It is advanced version o JavaScript, introduced to standardized JavaScript. It was published in 2015, that's the reason it is also called as ECMAScript2015. It is added some new features to JavaScript. Some popular features of the ECMAScript 6 are as follows…


 

Popular ES6 frameworks such as ReactJS, AngularJS, and Vue.js implemented these features

 

coding-zon-es6-classes-tutorial

 

ES6 Classes:

 
ES6 introduced classes.

A class starts with a class keyword.
Class contains constructor().
Inside, the constrictor properties are initialized.
 

class Person{
  constructor(name) {
    this.pname = name;
  }
}


Create an object called "pobj" based on the Car class:

 const pobj = new Person("John");
 
 
demo.html 
 
<!DOCTYPE html>
<html>

<body>
 
<script>
class Person{
  constructor(name) {
    this.pname = name;
  }
}

 const pobj = new Person("John");

document.write(pobj.pname );
</script>

</body>
</html>

Methods in Class 

Apart from the constructor, a Class contains some methods too.
Inside the class, you can add N number of methods of your own.

class Someclass{
  constructor() { ... }
  method1() { ... }
  method2() { ... }
  methodN() { ... }
}



class Person{
  constructor(name) {
    this.pname = name;
  }

displayName() {
    return 'Name: ' + this.name;
  }
}


Inheritance in Classes

A class can be inherited by using the extends keyword.

A class created with a class inheritance known as subclass and inherits all the methods from parent class:

class Employee extends Person{
  constructor(name, salary) {
    super(name);
    this.salary = salary;
  }  
  showDetails() {
      document.write( 'Name: '+this.displayName() +
 ', <br>Salary: ' + this.salary);
  }
  }
}
 
 
const pobj2 = new Employee("John", "25000");
pobj2.showDetails() ;
  
 
 

what is super() method in child class?

The super()  is a method used in a child class, that invokes a parent class.

By calling the super() method in the constructor method, we call the parent's constructor and get access to the parent's properties and methods.


demo2.html

<!DOCTYPE html>
<html>

<body>
 
<script>
class Person{
  constructor(name) {
    this.pname = name;
  }
  
  displayName()
  {
  return this.pname;
  }
  
}

class Employee extends Person{
  constructor(name, salary) {
    super(name);
    this.salary = salary;
  }  
  showDetails() {
      document.write( 'Name: '+this.displayName() + ', <br>Salary: ' + this.salary);
  }
} 

 const pobj = new Employee("John", "25000");
pobj.showDetails() ;
 
</script>
</body>
</html>

 
 
Name: John,
Salary: 25000 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP