用函数替换了class A
.当我用webpack构建时,我没有收到错误,但它不能正常工作.当然,它在class A
时工作得很好,但只有当它更改为function A
时才能正常工作,如下所示.最可疑的部分是init()
方法中的this.updateBind()
函数.它是类中的一个箭头函数,所以我认为当我将类更改为函数时可能会有问题.我不会上传整个源代码,因为我必须包括库,并与webpack建立它.这是因为/dist
文件夹中的main.js
和vender.js
文件很难阅读.我认为this.updateBind()
函数可能是导致问题的原因,我的猜测正确吗?在将class A
转换为function A
时,我应该如何处理箭头功能?
class A extends B {
constructor(m) {
super(m);
}
init() {
this.updateBind = () => {
this.update();
}
window.addEventListener('resizeEnd', this.updateBind);
}
update() {
this.scroll.update();
}
destroy() {
window.removeEventListener('resizeEnd', this.updateBind);
this.scroll.destroy();
}
}
用函数A替换A类的源代码
function A(m) {
B.call(this, m);
}
A.prototype.init = function() {
this.updateBind = () => {
this.update();
}
window.addEventListener('resizeEnd', this.updateBind);
}
A.prototype.update = function() {
this.scroll.update();
}
A.prototype.destroy = function() {
window.removeEventListener('resizeEnd', this.updateBind);
this.scroll.destroy();
}
A.prototype = Object.create(B.prototype);
A.prototype.constructor = B;