2017-10-31 28 views
1

「helpers」フォルダにhelpers.jsというファイルがあります。内容は以下のようなものです:ES6クラスで静的関数を呼び出せません

class Helpers { 
    constructor(config) { 
     if (this._singleton) { 
      throw new Error('A singleton has already been created.'); 
     } 

     this._singleton = this; 
    } 

    /** 
    * Gets the singleton object. 
    * @returns {Helpers} 
    */ 
    static getSingleton() { 
     return this._singleton; 
    } 
} 

module.exports = Helpers; 

はその後/helpers/user.jsに私はヘルパーのシングルトンインスタンスを取得したいです。 これは私のコードです:

const helpers = require('../helpers').getSingleton(); 

または

const Helpers = require('../helpers'); 
const helpers = Helpers.getSingleton(); 

私は入れませんエラーは次のとおりです。

TypeError: require(...).getSingleton is not a function 

または

TypeError: Helpers.getSingleton is not a function 

私はVSCodeにHelpers上にカーソルを移動した場合、私はgetSingleton()上にカーソルを移動するたびに、私はこのツールチップを取得し、このツールチップに

Helpers Tooltip

を取得し、:

getSingleton() tooltip

ので、パスが正しいですが、それはまだ私にエラーが発生します。

+0

としてそれを使用するあなたが持っているように、それが聞こえる: 'ヘルパー/ helpers.js'と'ヘルパー/ user.js'ので、 'require( './helpers')'や 'require( './helpers.js')'をしたいでしょう。これが問題ではないと仮定すると、 'console.log(タイプヘルパー)'は何を与えるのでしょうか? 'console.log(typeof Helpers.getSingleton)'はどうでしょうか? –

+2

エラーが表示されない場合でも、コードは機能しません。静的メソッド内の 'this'は、コンストラクタ内で' this'とは異なるものを参照します。これはシングルトンの仕組みではありません。 'getSingleton()'は新しいインスタンス(最初に呼び出されたとき)をインスタンス化して返します。 –

答えて

2

JavaScriptでシングルトンパターンを実装する最も簡単な方法は、クラスをまったくエクスポートしないことです。

class Helpers {} 

let helper; 
module.exports = function() { 
    if (!helper) helpers = new Helpers(); 
    return helper; 
}; 

// loaded with 
var helpers = require('../helpers')(); // note the extra() to call it 

またはより良い、我々は、Javaのような動作に制限されていないので、単に機能を完全にスキップしてください

class Helpers {} 
module.exports = new Helpers(); 

// loaded with 
var helpers = require('../helpers'); 

が、その後をあなたのモジュールがエクスポートしているすべてが単一である場合クラスのインスタンスでは、最初にクラスを使用する理由はほとんどありません。あなたにもあなたがSingleton.getInstance()としてそれを使用するには、このような何かを行うことができ

exports.helperMethodOne = function(){}; 
exports.helperMethodTwo = function(){}; 
exports.helperMethodThree = function(){}; 

// loaded with 
var helpers = require('../helpers'); 

または

module.exports = { 
    helperMethodOne() {}, 
    helperMethodTwo() {}, 
    helperMethodThree() {}, 
}; 

// loaded with 
var helpers = require('../helpers'); 
0

あなたのrequireステートメントは間違っていますが、環境を知らずに正しい構文を正確に伝えるのは難しいです。

const config = require('/path/to/file');

が典型的です。だから、試してみてください。

TypeError: require(...).getSingleton is not a function

require(...)nullのように、何か他のものに解決され、null.getSingletonですので:

const Helpers = require('../helpers');

を、あなたがエラーを取得し、あなたのスクリーンショットにない'../helpers'

'../helpers.js'を書きました関数ではありません。


また、thisを静的コンテキスト内に完全に参照することはできません。 thisは、静的メンバーではなくクラスインスタンスに対してのみ使用する必要があります。

+0

私は '../helpers'と' ../helpers.js'の両方を試しましたが、私が見て使ったものと違いはありません。 – Tvde1

+0

そして、 'require()'がnullを返した場合、 'undefined'エラーのgetSingleton 'プロパティを読み取れません。 – Tvde1

0

を行う可能性があります。

class Singleton { 
    static instance = new Singleton(); 
    static getInstance =() => Singleton.instance; 

    constructor() { 
     throw new Error('Use Singleton.getInstance()'); 
    } 
} 

module.exports = Singleton; 

またはそれ以上コソコソ何かとあなたの記述からnew Singleton()

class Singleton { 
    static instance; 
    constructor() { 
     if (!Singleton.instance) { 
      Singleton.instance = this; 
     } 
     return Singleton.instance; 
    } 
} 

module.exports = Singleton; 
関連する問題