0

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

1 Answer 1

2

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
    })
}
Sign up to request clarification or add additional context in comments.

2 Comments

great stuff thanks a lot :-) After seeing this work I was expecting to be able to created nested objects in the same way like pluginParameters[NS.property][blah] = 'xyz'; but that didn't seem to work. Instead I'm using pluginParameters[NS.property] = {blah:'xyz'}; which works ok.
@JamesSmith pluginParameters[NS.property] = {}; pluginParameters[NS.property][blah] = 'xyz';

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.