根据下面 ES6 构造函数的书写方式,写出 ES5 的class :
Example {
constructor(name) {
this.name = name;
}
init() {
const fun = () => {
console.log(this.name);
};
fun();
}
}
const e = new Example('Hello');
e.init();
实现方式如下:
function Example(name) {
this.name = name;
}
Example.prototype.init = function() {
var _this = this;
var fun = function() {
console.log(_this.name);
}
fun();
}
var e = new Example('Hello');
e.init();