TypeError . .不是构造函数

TypeError ... is not a Constructor

本文关键字:构造函数 TypeError      更新时间:2023-09-26

我有以下内容,这是一个解析器函数的开始。它依赖于jQuery扩展。我试图删除jQuery依赖,但当我这样做,我得到'VastParser不是一个构造函数'。我想我需要重新工作如何创建基本对象,使其工作。想法吗?

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();

根据注释,不需要构造函数,因为它是一个单例对象——

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