[英]Including Javascript file inside another Javascript file
I have two classes, say ParentClass
and ChildClass
in 2 separate files. 我有两个类,分别在2个单独的文件中说ParentClass
和ChildClass
。 I am directly invoking a function inside ChildClass
and I want to include the ParentClass
dynamically when the ChildClass
is invoked. 我直接在ChildClass
内部调用一个函数,并且想在调用ChildClass
时动态包含ParentClass
。 I DON'T want to use any external library or packages such as jQuery
, require.js
etc. Also I DON'T want to use ES6
or nodejs require
as said here , because of browser compatibility issues. 我不希望使用任何外部库或包如jQuery
, require.js
等。此外,我不希望使用ES6
或nodejs require
为说这里的,因为浏览器兼容性问题。
Here is how my files looks like, 这是我的文件的样子,
parentclass.js parentclass.js
var ParentClass = function(param1, param2) {
// Class related code here
};
ParentClass.prototype.parentClassFunction = function() {
// Code for the function here...
};
childclass.js childclass.js
var ChildClass = function(param1, param2) {
// Some class related code here...
this.parentClassFunction();
// Some other code...
};
ChildClass.prototype = Object.create(ParentClass.prototype);
HTML file HTML文件
.....
<head>
...
<script src="childclass.js"></script>
...
</head>
<body>
...
<script>
var value = new ChildClass(10, 20);
</script>
...
</body>
Is there any way by which I can achieve this? 有什么办法可以实现这一目标? Your help is appreciated. 感谢您的帮助。
Thanks in advance. 提前致谢。 :) :)
NOTE : I had a brief look into this , this and this question. 注意 :我对这个 , 这个和这个问题做了简短的研究。
Best option is to bundle all the files with a precompiler or something like that. 最好的选择是将所有文件与预编译器或类似的东西捆绑在一起。
in your childclass.js you should use require('parentclass.js') or import use something like Browserify to solve the dependencies automagically. 在childclass.js中,您应该使用require('parentclass.js')或import使用类似Browserify之类的方法自动解决依赖关系。
Here are some links: - http://browserify.org/ - https://webpack.github.io/ 这里是一些链接: -http : //browserify.org/-https : //webpack.github.io/
If you are use ES6 features you can use import: 如果您使用的是ES6功能,则可以使用导入:
for example: 例如:
import { Childclass } from '/path/to/childclass';
Here is the documentation for import: 这是要导入的文档:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.