17
Apr

Javascript classes – another example

function MyClass(){
this.name = “Sheetal”;
this.address = “12311, Springwater Pt.”
};

MyClass.prototype.setName = function(name){
this.name = name;
};

MyClass.prototype.getName = function(){
return this.name;
};

var y = new MyClass();
y.setName(“Sheetala”);
console.log(y.getName());

output:
——–
Sheetala

ES2015 Classes
In the ES2015 specification, you can use the class syntax which is just sugar over the prototype system.

class Person {
constructor(name) {
this.name = name;
}
toString() {
return `My name is ${ this.name }.`;
}
}

class Employee extends Person {
constructor(name, hours) {
super(name);
this.hours = hours;
}
toString() {
return `${ super.toString() } I work ${ this.hours } hours.`;
}
}