簡體   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