简体   繁体   中英

TypeError ... is not a Constructor

I have the following, which is a start of a parser function. It has a dependency on jQuery extend. I am trying to remove the jQuery dependency but when I do, I get 'VastParser is not a constructor'. Think I need to rework how to create base object to make it work. Thoughts?

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

Per comment, constructor is not needed since its a singleton object --

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

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

VASTParser.parse({test: 'test xml'});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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