[英]Including Javascript file inside another Javascript file
我有两个类,分别在2个单独的文件中说ParentClass
和ChildClass
。 我直接在ChildClass
内部调用一个函数,并且想在调用ChildClass
时动态包含ParentClass
。 我不希望使用任何外部库或包如jQuery
, require.js
等。此外,我不希望使用ES6
或nodejs require
为说这里的,因为浏览器兼容性问题。
这是我的文件的样子,
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/
如果您使用的是ES6功能,则可以使用导入:
例如:
import { Childclass } from '/path/to/childclass';
这是要导入的文档:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.