【发布时间】:2021-02-25 15:59:36
【问题描述】:
假设我为辅助方法构建了一个库类。 我有一个包含字符串 utils 的文件,一个包含 ajax utils 的文件等等。
主类看起来像这样:
// helperUtils.js
export class helperUtils {
constructor() {}
basicHelperFn() {
return true;
}
}
现在我有一个导出这些字符串工具的文件:
// helperUtils/stringUtils.js
import { helperUtils } from '../helperUtils.js';
helperUtils.prototype = {
stringUtil1 : (str) => {
return str += ' this is very helpful!';
}
}
export const stringUtils = helperUtils.prototype;
// more files...
// helperUtils/ajaxUtils.js
// helperUtils/objUtils.js
// ...
我现在如何导入主类并扩展它,仅将一些原型方法拆分为多个文件,例如仅导入字符串 utils。
import { helperUtils } from './helperUtils';
import { stringUtils} from './helperUtils/stringUtils';
const helper = new helperUtils();
helper.basicHelperFn();
helper.stringUtil1();
【问题讨论】:
标签: javascript class inheritance ecmascript-6 prototype