如何使用window对象(或更好的方法)来创建动态属性名称

125*_*748 0 javascript jquery

在这里读到窗口对象可以用来动态地在对象中创建属性名称.这是怎么做的

我有一个像这样的功能

function storeValues(){

    var info = {};
    $('.class1').each(function(index1, value1){
        $('.class2', $(this)).each(function(index2, value2){

            //so here I'd like to add a string to the property name
            //like 'param-' and 'detail-' so I could end up with a 
            //structure like 
            //info.param_0.detail_0
            //then
            //info.param_0.detail_1
            //info.param_1.detail_0
            //info.param_1.detail_1
            //info.param_1.detail_2
            info.'param_'index1.'detail_'index2 = $(this).find('.desired_input').val();

    });
}
Run Code Online (Sandbox Code Playgroud)

这可能吗.或者有更聪明的方法吗?

Fab*_*tté 7

这与访问动态对象属性名称window[]符号无关,这是JavaScript语言的一个方面.

info['param_' + index1]['detail_' + index2] = $(this).find('.desired_input').val();
Run Code Online (Sandbox Code Playgroud)

当然,如果info['param_' + index1]还不存在,则必须在设置其属性之前将其创建为空对象.

info['param_' + index1] = info['param_' + index1] || {};
info['param_' + index1]['detail_' + index2] = $(this).find('.desired_input').val();
Run Code Online (Sandbox Code Playgroud)