我有两个类,分别为ParentClass
和ChildClass
,分为两个单独的文件。我直接调用ChildClass
中的函数,并且我想在调用ChildClass
时动态地包含ParentClass
。我请勿要使用的任何外部库或包如jQuery
,require.js
等。此外,我请勿要使用ES6
或nodejs require
如说here,因为浏览器兼容性问题。将JavaScript文件包含在另一个Javascript文件中
这里是我的文件看起来像,
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>
有没有什么办法让我能做到这一点?感谢您的帮助。
在此先感谢。 :)
注意:我有一个简单回顾一下this,this和this问题。
或使用类似webpack的打包器 –
@BalajMarius我已经在
标记内导入了脚本。我将以更清晰的方式编辑问题。 – bharadhwaj
也许写