javascript - 从构造函数调用 JS 原型函数(ES6 模块)
问题描述
我正在重写一些作为ES6模块的JavaScript脚本代码,并且在调用原型函数时遇到问题。
原始脚本的结构是这样的:
function randomThing(a) {
this.a = a
this.doRandomThing()
..
}
randomThing.prototype.doRandomThing = function () { ... }
但是,调用与此。不再在模块中工作。
export default function randomThing(a) {
this.a = a
this.doRandomThing() // error: this.doRandomThing is not a function
..
}
randomThing.prototype.doRandomThing = function () { ... }
如何从构造函数中调用doRandomThing()函数?我想我可以在构造函数中将它定义为 var,但是有没有办法使用.prototype语法来保持版本?
解决方案
推荐阅读
- ruby - Ruby:我使用的范围正确吗?
- python - 压缩时无法保持分拣稳定性
- kubernetes - 如何将动态值传递给 Kubernetes 容器
- html - 在 HTML/CSS 中的输入上覆盖文本
- openssl - 错误:作业失败:退出代码 1 - 解密错误
- php - zval 中 zend_refcounted 是如何使用的?
- android - 在 Kotlin Android Studio 中缩放图像大小
- java - 与 Heroku 应用程序的直接 TCP 连接
- numpy - 如何将 .npz 文件加载到 Google Compute Engine
- django - 带有 Nginx 的 Django 的 build_absolute_uri 中的 localhost