ES6 JS 类与继承、静态方法

2018-04-09  本文已影响19人  凌雲木

一 类

console.log("----------------------ES6以前的语法--------------------------------------------------");
//ES6以前类的创建及向类中追加一个属性与一个方法
function employee(name, job, born) {
this.name = name;
this.job = job;
this.age = born;
this.show = function (obj) {
console.log("hahaha" + obj);
};
}
var bill = new employee("Bill Gates", "Engineer", 1985);
//向类中追加一个属性与一个方法
employee.prototype.salary = 21421;
employee.prototype.test = function (obj) {
console.log(obj);
return "hello" + obj
};
bill.test("钉钉");
console.log(bill);

继承

console.log("--------------------------ES6类的继承----------------------------------------------");
class Teacher extends Person {
constructor(x, y, color) {
super(x, y); // 调用父类的Person(x, y)
this.color = color;
}
toString() {
return this.color + ' ' + super.toString(); // 调用父类的toString()
}
}
var teacher = new Teacher("旺财", 5, "Red");
console.log(teacher);

静态方法

class School {
static classMethod() {
return '机械一班';
}
}
console.log(School.classMethod());
var school = new School();
console.log(school.classMethod());


image.png
上一篇 下一篇

猜你喜欢

热点阅读