繁体   English   中英

在另一个Javascript文件中包含Javascript文件

[英]Including Javascript file inside another Javascript file

我有两个类,分别在2个单独的文件中说ParentClassChildClass 我直接在ChildClass内部调用一个函数,并且想在调用ChildClass时动态包含ParentClass 希望使用任何外部库或包jQueryrequire.js等。此外,我希望使用ES6nodejs 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM