Class in pre-ES6
var employee = {
name: null,
age: null,
setName: function (name) {
this.name = name;
},
getName: function () {
return this.name;
},
setAge: function (age) {
this.age = age;
},
getAge: function () {
return this.age;
}
};
Class in ES6
class Employee {
constructor (name, age) {
this.name = name;
this.age = age;
}
setName (name) {
this.name = name;
}
getName () {
return this.name;
}
setAge (age) {
this.age = age;
}
getAge () {
return this.age;
}
};
class MaleEmployee extends Employee {
constructor (name,age,wifeName) {
super(name,age);
this.wifeName = wifeName;
}
setWifeName (wifeName) {
this.setWifeName;
}
getWifeName () {
return this.wifeName;
}
classClassName () {
return super.getClassName();
}
}
var employee = new MaleEmployee();
console.log(employee.classClassName());
//Result: Class Employee
Class in Typescript
class Person {
protected name: string;
constructor(name: string) { this.name = name; }
}
class Employee extends Person {
private department: string;
constructor(name: string, department: string) {
super(name);
this.department = department;
}
public getElevatorPitch() {
return `Hello, my name is ${this.name} and I work in ${this.department}.`;
}
}
let howard = new Employee("Howard", "Sales");
console.log(howard.getElevatorPitch());
console.log(howard.name); // error
Top comments (0)