繁体   English   中英

TypeError ... 不是构造函数

[英]TypeError ... is not a Constructor

我有以下内容,这是解析器函数的开始。 它依赖于 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'});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM