美文网首页
前端面试js篇

前端面试js篇

作者: 5cc9c8608284 | 来源:发表于2024-03-20 08:19 被阅读0次

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

相关文章

网友评论

      本文标题:前端面试js篇

      本文链接:https://www.haomeiwen.com/subject/oecxtjtx.html