我有两个类,例如 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>
<小时/>
有什么办法可以实现这个目标吗?感谢您的帮助。
提前致谢。 :)
最佳答案
最好的选择是将所有文件与预编译器或类似的东西捆绑在一起。
在你的 childclass.js 中,你应该使用 require('parentclass.js') 或 import 使用像 Browserify 这样的东西来自动解决依赖关系。
以下是一些链接: -http://browserify.org/ -https://webpack.github.io/
关于javascript - 将 Javascript 文件包含在另一个 Javascript 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43227791/