2016-09-12 10 views
-2

以下は、パーサー関数の開始点です。それはjQueryの拡張に依存しています。私はjQueryの依存関係を削除しようとしていますが、私が行ったとき、私は 'VastParserはコンストラクタではありません。それを動作させるために基本オブジェクトを作成する方法を再作ってみる必要があると思う。思考?コメントパーTypeError ...はコンストラクターではありません

var VASTParser = $.extend(function(){}, { 
    constructor: function() { 
    // only work once... 
    if (VASTParser._instance) { 
     this.parse = null; 
     this.parseVast = null; 
     throw "VASTParser is a Singlton. Access via getInstance()"; 
    } 

    VASTParser._instance = this; 
    console.log("VASTParser instantiated.", "", "VAST"); 
    }, 
    parse: function(xml) { 
    console.log(xml); 
    } 
}); 

// create static getInstance() 
VASTParser.getInstance = function() { 
    if (!VASTParser._instance) { 
    VASTParser._instance = new VASTParser(); 
    } 
    console.log(VASTParser._instance); 
    return VASTParser._instance; 
}; 

// call it, to prevent the constructor from being succeeding via direct calls again 
VASTParser.getInstance(); 
+1

VASTParser'は、オブジェクト、関数ではなく 'ためです。シングルトンを作成しているので、コンストラクタをまったく持っている必要はありません。ただあなたのオブジェクトを作ると、あなたは完了です。 –

+0

私は参照してください。この場合、コンストラクタとgetInstanceを削除できますか?このようなもの... var VASTParser = { parse:function(xml){ console.log(xml); } }; VASTParser.parse = function(xml){ return xml; }; VASTParser.parse({test: 'test xml'}); – Yasir

+0

それは最高です。 –

答えて

0

、コンストラクタは、そのシングルトンオブジェクト以来不要です -

var VASTParser = { 
    parse: function(xml) { 
    console.log(xml); 
    } 
}; 

VASTParser.parse = function(xml) { 
    return xml; 
}; 

VASTParser.parse({test: 'test xml'}); 
関連する問題