简体   繁体   English

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

[英]Including a JavaScript file in another JavaScript file

I am a newbie in developing JavaScript libraries. 我是开发JavaScript库的新手。 I've a basic doubt on how to include a js file into another js file that is dependent on the former. 对于如何将js文件包含到另一个依赖于前者的js文件中,我有一个基本的疑问。 For example, I'm building a javascript library that replicates the class structures present in the Server code.There are cases for example, where class B is contained in class A . 例如,我正在构建一个JavaScript库来复制服务器代码中存在的类结构。例如,在某些情况下,类B包含在类A I want to make a seperate js file to denote class B and then include this in the definition of class A . 我想制作一个单独的js文件来表示类B ,然后将其包含在类A的定义中。 How do I go about this ? 我该怎么办?

I appreciate your help. 我感谢您的帮助。

Thanks! 谢谢!

You can define a function in b.js which returns B class (function) and call it inside class A 您可以在b.js中定义一个函数,该函数返回B类(函数)并在A类内部调用

b.js
getClassB function (){
    //define your B class here and return it at the end
    return B;
}

a.js
class/function A(){
    var B = getClassB();
}

If you are using it in html page, simply import class A file first and then class B file like 如果您在html页面中使用它,只需先导入A类文件,然后再导入B类文件,例如

<script src="path_to_class_a_file"></script>
<script src="path_to_class_b_file"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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