user516883
user516883

Reputation: 9378

javascript calling init function after setup

I am trying to create a js object, I want either the user passes in some json data or the objects properties are set by default as shown below. After that is all set, I finally want to call an init function that runs and does the work. Thanks for any help. I am not trying to create a jQuery plugin.

var PictureDialog = function (settings) {
    settings = {
        allowShortKey: true,
        prevID: null,
        prevCounterNumber: null,
        startValue: 0,
        nextValue: 1,
        init: function() {
            //Do work
           //Show dialog
        }
    },settings;

}

Would the call look something like this

PictureDialog({prevID:1}).init();

Upvotes: 1

Views: 15397

Answers (3)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76397

The quick and simple way would be this:

var PictureDialog = function (settings)
{
    settings = {
        allowShortKey: true,
        prevID: null,
        prevCounterNumber: null,
        startValue: 0,
        nextValue: 1,
        init: function()
        {
            //Do work
            //Show dialog
            return this;//<--return object, too
        }
    };
    return settings;//return the object
};
foo = PictureDialog().init();//the init will be called on the return value of PictureDialog

I don't, however, get why the PictureDialog function expects a settings argument. Either pass nothing to the function at all, or alter the passed value:

var PictureDialog = (function()
{
    var defaults = {
        allowShortKey: true,
        prevID: null,
        prevCounterNumber: null,
        startValue: 0,
        nextValue: 1,
        init: function()
        {
            //Do work
           //Show dialog
           return this;//<--Very important
        };
    return function (settings)
    {
        settings = settings instanceof Object ? settings : {};
        for (var n in defaults)
        {
            if (defaults.hasOwnProperty(n))
            {//set all properties that are missing from argument-object
                settings[n] = settings[n] || defaults[n];
            }
        }
        return settings;
    };
}());
foo = PictureDialog({allowShortKey: false}).init();//will return full settings object

Upvotes: 0

elclanrs
elclanrs

Reputation: 94101

If I understand correctly you want to set some default values, with the help of jQuery you can use $.extend like this:

function Foo( settings ) {
  var _defaults = { a: 'a', b: 'b' };
  settings = $.extend( settings, _defaults );
}

With pure JavaScript you'd have to do something like this:

function Foo( settings ) {
  var _defaults = { a: 'a', b: 'b' };
  settings.a = settings.a || _defaults.a;
  settings.b = settings.b || _defaults.b;
}

As for the init method, you can just add it to the prototype and execute it in the constructor so you don't even have to call it when you create a new instance:

function Foo( settings ) {
  var _defaults = { a: 'a', b: 'b' };
  ...
  this.init();
}

Foo.prototype = {
  init: function() {
    console.log('initialized!');
  }
}

Upvotes: 0

user123444555621
user123444555621

Reputation: 152976

Not sure why you would need an init function at all. This is how I would do it:

(function () {
    var defaultSettings = {
        allowShortKey: true,
        prevID: null,
        prevCounterNumber: null,
        startValue: 0,
        nextValue: 1
    };
    PictureDialog = function (settings) {
        settings = settings || defaultSettings;
        //Do work
        //Show dialog
    };
})();

The outer function is just to make sure that defaultSettings doesn't pollute the global scope.

Upvotes: 1

Related Questions