ES6 类与模块、Promise 与代理全解析
一、ES6 类
JavaScript 是基于原型的语言,支持原型继承。ES6 引入了类的概念,对于熟悉传统面向对象语言(如 Java)的开发者来说,会感到很亲切。但实际上,JavaScript 中的类只是对原型继承的语法糖。
1. ES5 与 ES6 类语法对比
- ES5 原型继承示例
var Person = function(firstname) {
if (!(this instanceof Person)) {
throw new Error("Person is a constructor");
}
this.firstname = firstname;
};
Person.prototype.giveBirth = function() {
// ...we give birth to the person
};
var Employee = function(firstname, lastname, job) {
if (!(this instanceof Employee)) {
throw new Error("Employee is a constructor");
}
Person.call(this, firstname);
this.job = job;
};
Employee.prototype = Object.create(Person.prototy