在另一个Javascript文件中包含Javascript文件
我有两个类,比如在另一个Javascript文件中包含Javascript文件,javascript,Javascript,我有两个类,比如ParentClass和ChildClass在两个单独的文件中。我直接调用ChildClass中的函数,我希望在调用ChildClass时动态地包含ParentClass。我不想使用任何外部库或包,如jQuery,require.js等。由于浏览器兼容性问题,我也不想使用ES6或nodejsrequire 这是我的文件的样子 parentclass.js var ParentClass = function(param1, param2) { // Class rela
ParentClass
和ChildClass
在两个单独的文件中。我直接调用ChildClass
中的函数,我希望在调用ChildClass
时动态地包含ParentClass
。我不想使用任何外部库或包,如jQuery
,require.js
等。由于浏览器兼容性问题,我也不想使用ES6
或nodejsrequire
这是我的文件的样子 parentclass.js
var ParentClass = function(param1, param2) {
// Class related code here
};
ParentClass.prototype.parentClassFunction = function() {
// Code for the function here...
};
var ChildClass = function(param1, param2) {
// Some class related code here...
this.parentClassFunction();
// Some other code...
};
ChildClass.prototype = Object.create(ParentClass.prototype);
childclass.js
var ParentClass = function(param1, param2) {
// Class related code here
};
ParentClass.prototype.parentClassFunction = function() {
// Code for the function here...
};
var ChildClass = function(param1, param2) {
// Some class related code here...
this.parentClassFunction();
// Some other code...
};
ChildClass.prototype = Object.create(ParentClass.prototype);
HTML文件
.....
<head>
...
<script src="childclass.js"></script>
...
</head>
<body>
...
<script>
var value = new ChildClass(10, 20);
</script>
...
</body>
。。。。。
...
...
...
var值=新的子类(10,20);
...
我有什么办法可以做到这一点吗?谢谢你的帮助 提前感谢。:)
注意:我做了一个简短的调查,并提出了一个问题。如果您正在使用ES6功能,您可以使用导入: 例如:
从'/path/导入{Childclass}到/Childclass'代码>
以下是用于导入的文档:
最好的选择是使用预编译器或类似的东西捆绑所有文件
在childclass.js中,您应该使用require('parentclass.js')或import,或者使用类似Browserify的东西来自动解决依赖关系
以下是一些链接:
-
-可以用或使用类似bundler的工具编写脚本webpack@BalajMarius我已经导入了标签内的脚本。我将以更清晰的方式编辑这个问题。@bharadhwaj你所说的“.”是什么意思?我想在调用ChildClass时动态地包括ParentClass“@BalajMarius我的意思是,当调用ChildClass
时,它使用ParentClass
中的函数,但我不知道如何在HTML中手动添加每个依赖的js文件,而是希望在需要时动态选择它。希望这是清楚的。我在问题中提到过,我不想要ES6是因为浏览器兼容性问题。哦,很抱歉,为什么你不尝试使用commonjs来像nodejs那样拥有需求:我正在尝试以最低的要求创建一个库,所以我不想增加包的开销。这就是原因。