Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在多个文件中拆分ES6类以导入库的各个方法_Javascript_Class_Inheritance_Ecmascript 6_Prototype - Fatal编程技术网

Javascript 在多个文件中拆分ES6类以导入库的各个方法

Javascript 在多个文件中拆分ES6类以导入库的各个方法,javascript,class,inheritance,ecmascript-6,prototype,Javascript,Class,Inheritance,Ecmascript 6,Prototype,假设我为助手方法构建了一个库类。 我有一个包含字符串utils的文件,一个包含ajax utils的文件,等等 主类如下所示: //helperUtils.js 导出类helperUtils{ 构造函数(){} basicHelperFn(){ 返回true; } } 现在我有了一个导出这些字符串util的文件: //helperUtils/stringUtils.js 从“../helperUtils.js”导入{helperUtils}; helperUtils.prototype={ s

假设我为助手方法构建了一个库类。 我有一个包含字符串utils的文件,一个包含ajax utils的文件,等等

主类如下所示:

//helperUtils.js
导出类helperUtils{
构造函数(){}
basicHelperFn(){
返回true;
}
}
现在我有了一个导出这些字符串util的文件:

//helperUtils/stringUtils.js
从“../helperUtils.js”导入{helperUtils};
helperUtils.prototype={
stringUtil1:(str)=>{
return str+=“这非常有用!”;
}
}
export const stringUtils=helperUtils.prototype;
//更多文件。。。
//helperUtils/ajaxUtils.js
//helperUtils/objUtils.js
// ...
我现在如何导入main类并用几个文件中分割的一些原型方法扩展它,例如只导入字符串utils

从“/helperUtils”导入{helperUtils};
从“./helperUtils/stringUtils”导入{stringUtils};
const helper=new helperUtils();
helper.basicHelperFn();
helper.stringUtil1();
如果您的stringUtils类是helperUtils类:

// helperUtils/stringUtils.js
import { helperUtils } from '../helperUtils.js';

class StringUtils extends helperUtils {
    stringUtil1(str) {
         return str += ' this is very helpful!';
    }
}

export StringUtils;
然后,您只需导入并使用它。

如果您的stringUtils类是helperUtils类:

// helperUtils/stringUtils.js
import { helperUtils } from '../helperUtils.js';

class StringUtils extends helperUtils {
    stringUtil1(str) {
         return str += ' this is very helpful!';
    }
}

export StringUtils;
然后您可以简单地导入它并使用它。

类似这样的东西

/*
helperUtils/stringUtils.js
*/
从“../helperUtils.js”导入{helperUtils};
类StringUtils扩展了helperUtils{
超级();
构造函数(){}
stringUtilNew(x){
/*
做点什么。。。
*/
返回x;
}
}
出口纺织品

/*
helperUtils/stringUtils.js
*/
从“../helperUtils.js”导入{helperUtils};
类StringUtils扩展了helperUtils{
超级();
构造函数(){}
stringUtilNew(x){
/*
做点什么。。。
*/
返回x;
}
}

出口纺织品我做了更多的实验:

我现在想到的是:

//helperUtils/stringUtils.js
从“../helperUtils.js”导入{helperUtils};
helperUtils.prototype.stringUtil1=(str)=>{
return str+=“这非常有用!”;
}
export const stringUtils=helperUtils.prototype;
现在stringUtils原型不会覆盖我的helperUtils原型,我可以安全地将单个方法导入我的主类

如果有人有更好的解决方案,请与我们分享


谢谢你

我做了更多的实验:

我现在想到的是:

//helperUtils/stringUtils.js
从“../helperUtils.js”导入{helperUtils};
helperUtils.prototype.stringUtil1=(str)=>{
return str+=“这非常有用!”;
}
export const stringUtils=helperUtils.prototype;
现在stringUtils原型不会覆盖我的helperUtils原型,我可以安全地将单个方法导入我的主类

如果有人有更好的解决方案,请与我们分享


谢谢你

super()不应该在构造函数或其他方法中调用吗?再加上使用扩展类,我必须调用子类来访问这些方法,对吗?super()不应该在构造函数或其他方法中调用吗?另外,使用扩展类,我必须调用子类来访问这些方法,对吗?
super()
位于错误的位置,它和构造函数都可以删除。谢谢。修复了
super()
位于错误位置的问题,并且可以删除它和构造函数。谢谢。修好了