【发布时间】:2023-03-18 21:54:01
【问题描述】:
我有两个类,例如 ParentClass 和 ChildClass 在 2 个单独的文件中。我直接在ChildClass 中调用一个函数,并且我想在调用ChildClass 时动态包含ParentClass。我不想要使用任何外部库或包,例如jQuery、require.js 等。我也不想要使用ES6 或nodejs require 如here 所说,因为浏览器兼容性问题。
这是我的文件的样子,
parentclass.js
var ParentClass = function(param1, param2) {
// Class related code here
};
ParentClass.prototype.parentClassFunction = function() {
// Code for the function here...
};
childclass.js
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>
有什么方法可以实现吗?感谢您的帮助。
提前致谢。 :)
【问题讨论】:
-
或者使用像 webpack 这样的打包工具
-
@BalajMarius 我在 标签中导入了脚本。我会以更清晰的方式编辑问题。
-
@bharadhwaj “..我想在调用 ChildClass 时动态包含 ParentClass”是什么意思
-
@BalajMarius 我的意思是,当
ChildClass被调用时,它使用ParentClass中的函数,但我不想在HTML 中手动添加每个依赖的js 文件,而是想动态选择它需要的时候。希望清楚。
标签: javascript