2017-04-05 85 views
0

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

有没有什么办法让我能做到这一点?感谢您的帮助。

在此先感谢。 :)

注意:我有一个简单回顾一下thisthisthis问题。

+0

也许写脚本http://stackoverflow.com/questions/18784920/how-to-add-dom-element-script-to-头部 – 2017-04-05 09:57:17

+0

或使用类似webpack的打包器 –

+0

@BalajMarius我已经在标记内导入了脚本。我将以更清晰的方式编辑问题。 – bharadhwaj

回答

1

最好的选择是将所有文件与预编译器或类似的东西捆绑在一起。

在您的childclass.js中,您应该使用require('parentclass.js')或导入使用类似于Browserify的东西来自动解决依赖关系。

这里有一些链接: - http://browserify.org/ - https://webpack.github.io/

相关问题