简体   繁体   中英

javascript key i18n

i have a code something like this.

var myI18n; // json : "{"close":"بستن","locationInformation":"مشخصات جغرافیایی","yes":"بله","no":"خیر"}"

and i want to use in somthing like this.

$.modal({
    buttons: {
        myI18n.close: function(win) {

        }
    }
});

but i have a syntax error. also with this method string not change.

var aaa = myI18n.close;
$.modal({
    buttons: {
        aaa: function(win) {

        }
    }
});

The only way to assign properties with dynamic names to an object is to use array syntax

 var myButtons = {};
 myButtons[myi18n.close] = ...

You can't reference variables in key names in object literal, so you need to create your object first, fill it using regular [] notation and then just use it in $.modal .

buttons = {}

buttons[myI18n.close] = function(win) {
   // ....
}

$.modal({
    buttons: buttons
});

You can't use an object literal directly with a variable key. You'll need to separate out your code to use the array access syntax ( obj[keyname] = value ).

var obj = {
        buttons: {}
    };
obj.buttons[myI18n.close] = function () {...};
$.modal(obj);

Alternatively, you could use a module pattern:

$.modal({
    buttons: (function () {
        var btns = {};
        btns[myI18n.close] = function () {...};
        return btns;
    }())
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM