James Smith
James Smith

Reputation: 538

Javascript object literal passing properties to jquery plugin options

This is one of those problems that's been bothering me for a while but I always just worked around it without truly figuring out a proper solution... Apologies if it has been answered before but I couldn't find an answer. If at all possible I'd like to avoid refactoring the object literal pattern.

In the following example, I can't access NS.something and I'm not sure why...

var NS = {
    something : 'abc',

    init : function(){
        NS.doSomething();
    },

    doSomething : function(){
        $('.elements').jqueryPlugin({
            pluginParameters: {
                NS.something : 'xyz';
            }
        })
    }
};

NS.init();

Upvotes: 0

Views: 117

Answers (1)

Musa
Musa

Reputation: 97672

You cannot define an object literal with a variable key, you have to assign it after definition with [] notation.

doSomething : function(){
    var pluginParameters = {};
    pluginParameters[NS.property] = 'xyz';
    $('.elements').jqueryPlugin({
        pluginParameters: pluginParameters
    })
}

Upvotes: 2

Related Questions