一 类
-
ES6以前的语法
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);
-
ES6的语法
console.log("--------------------------ES6的语法----------------------------------------------");
//ES6类定义类
class Person {
// 构造函数
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return (this.x + "的年龄是" + this.y + "岁");
}
}
//Person如C#一样,需要用new实例化
var model = new Person("xiaoming", 12);
console.log(model.toString());
二 继承
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());