問題は、NodeJSモジュールからエクスポートされたクラスの静的メソッドから他の静的メソッドを参照する方法ですか?ここにセットアップがあります。私は以下のモジュールがあります。 ノードJSモジュールからエクスポートされたクラスの静的メソッドの参照
var Parent = class {
static smethod1() {
return this.smethod2();
}
static smethod2() {
return "Testing tube";
}
}
module.exports = {
Parent:Parent
}
test1.js
test2.js
require
そのSこのモジュール
var mod = require('./test1');
var Parent = mod.Parent;
module.exports = {
sm1: Parent.smethod1,
sm2: Parent.smethod2
}
最後に、私はrun.js
var test2 = require('./test2');
console.log(test2.sm1());
当然で実行されているコードを持って、私は"Testing tube"
ラインが印刷されて見たいと思う。私はもちろん、エラー
return this.smethod2();
^
TypeError: this.smethod2 is not a function
を取得しています、そこthis
はモジュールを指しNodeJS上のペテンだが、それは代わりに、関数に参照すべきではありませんか?現在の設定でsmethod1
から静的メソッドsmethod2
を参照する方法はありますか?そうでない場合は、どのような回避策がありますか?
あなたはこの答えの表情を持つことができます。http://stackoverflow.com/a/28979516/1681972 – Ravi
あなただけの静的メソッドを持つクラスを持っている場合、これはあなただけの関数をエクスポートする必要がありサインです直接とdクラスをelete、FYI。 – loganfsmyth
@loganfsmythこれはもちろん簡単です。 – EvgeniySharapov