Ecmasript 6 ile birlikte class keywordü geliyor. Aslında arkaplanda prototype’ın içerisine methodları gömerek bizim elle yapmamız gereken tüm prototype işlemlerini yapmış oluyor.

Örneğin ; 

function Employee(name,surname,age) {
this.name = name;

this.surname = surname;

this.age = age;
}

Eğer Employee function’i içerisine bir method yazarsak Employee’den oluşturulan her bir nesne için boşu boşuna o method tekrar tekrar oluşturulur. Bunun yerine onu prototype içerisine gömeriz. Ve tüm nesneler için tek bir function oluşturulmuş olur.

Employee.prototype.printFunction = function () {

console.log(this.name , this.surname);
}

Eğer ES6 ile gelen class keyword’unu kullanırsak o bizim yerimize bu işlemi yapmış olur. Yani ES6′ da bizim için direk sınıf içerisine yazdığımız methodu prototype içerisine alıyor.Aşağıda yazmış olduğumuz kod bloğu da yukarıdaki kod ile aynı şeyi yapmış olur.

class Person {
constructor(name,surname,age) {
this.name = name;
this.surname = surname;
this.age = age;
}

printFunction () {
console.log(this.name + this.surname);
}
}

Static Methodlar

ES6 ile beraber gelmiştir. Bazen obje oluşturmadan direk sınıf ismi ile methodları çağırmak isteyebiliriz. ES6 ile bu destekleniyor.  Function başına static yazmak yeterlidir.

Object.create() , Math.sqrt() gibi methodlarda static methodlardır.

ES6 Ile Kalıtım Örneği

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

showInfos() {
console.log(this.name + this.age);
}
}

class Employee extends Person() {
constructor(name,age,salary){
//this.name = name;
//this.age=age;
super(name,age);
this.salary=salary;
}

showInfos(){ //override method
console.log(this.name,this.age,this.salary);
}

}