前端面试js篇

2024-03-20  本文已影响0人  5cc9c8608284

根据下面 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();
上一篇 下一篇

猜你喜欢

热点阅读