【发布时间】:2018-02-14 14:30:16
【问题描述】:
我想在单独的 .js 文件中为特定类定义方法。然后应该在运行时动态读取文件,并在实例化之前将方法添加到类中。
示例方法.js:
function method1() {
console.log("first method");
}
function method2() {
console.log("second method");
}
示例 runtime.js:
class myClass { }
jsContent = fopenContent //already includes the content of methods.js from php server via fopen
//the methods should be added to myClass here (before instantiating)
newClass = new myClass();
newClass.method1();
我们的目标是拥有一个带有简单方法的基本类。当我在不同的 html 页面上实例化它时,我需要使用更复杂的方法扩展该基本类。
【问题讨论】:
-
这看起来真的很奇怪你想解决什么问题?这闻起来像the XY problem
-
你可以在你的 js 文件中做
myClass.prototype = { method1: function() { console.log("first method"); }, method2: function() { console.log("second method"); } };但这真的很尴尬
标签: javascript file class methods