1

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();
3
  • 1
    You seem to have a syntax error, what's ,settings doing there alone? Commented Oct 29, 2012 at 5:13
  • Beyond the syntax error, is this even the proper way to set this up? Commented Oct 29, 2012 at 5:14
  • If you want to chain an .init() call on the end of the PictureDialog() call then your PictureDialog() function needs to return an object that has an .init() method. Commented Oct 29, 2012 at 5:24

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Just simply PictureDialog({prevID:1});
0

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!');
  }
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.