[英]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.