user761100 user761100 - 1 year ago 83
Javascript Question

Trouble defining Constructor in Javascript

I have a Javascript object in common.js file:

var KITS=
debug: false,
default_country : 'xxx',
franchise : ''

var self = this;
// ...

if (typeof cb==='function') (cb)();

if (typeof cb==='function') (cb)();

I include this js file in a php file. Could some expert please advise what I need to do so that KITS.init() is automatically called when the KITS object is created?

Answer Source

Object literals are not created with new, so they wouldn't trigger the constructor even if they had one. They do not have constructors, thanks to not having a prototype of their own:

const foo = {
  a: 1,
  b: 2

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download