如何从JavaScript中的对象键值返回数组?

时间:2022-08-26 14:07:05

So I have the following piece of code:

所以我有以下的一段代码:

var structures = {

    loginStructure : function(){

        return structure = [
            '<form name="',opts.formClass,'" class="',opts.formClass,'" method="post" action="#">',
                '<fieldset class="',opts.fieldsWrapper,'">',
                    '<fieldset class="',opts.userWrapper,'">',
                        '<label for="',opts.userInt,'" class="',opts.userLbl,'"><img src="',opts.userIcon,'" alt="',opts.userName,'" /></label>',
                        '<input type="text" name="',opts.userInt,'" class="',opts.userInt,'" placeholder="',checkNameLenght(opts.userName,namesLenght.userNameLenght,16,'Username'),'" value="" autocomplete="off" />',
                    '</fieldset>',
                    '<fieldset class="',opts.passWrapper,'">',
                        '<label for="',opts.passInt,'" class="',opts.passLbl,'"><img src="',opts.passIcon,'" alt="',opts.passName,'" /></label>',
                        '<input type="password" name="',opts.passInt,'" class="',opts.passInt,'" placeholder="',checkNameLenght(opts.passName,namesLenght.passNameLenght,16,'Password'),'" value="" autocomplete="off" />',
                    '</fieldset>',
                    '<fieldset class="',opts.btnWrapper,'">',
                        '<button type="submit" name="',opts.btnInt,'" class="',opts.btnInt,'">',checkNameLenght(opts.btnName,namesLenght.btnNameLenght,7,'Login'),'</button>',
                    '</fieldset>',
                '</fieldset>',
                '<div class="toogle-button">',
                    '<ul class="inside">',
                        '<li class="toogle"><a><img src="assets/gfx/toogle.png" alt="Back" /></a></li>',
                    '</ul>',
                '</div>',
            '</form>',
            '<div class="toogle-buttons">',
            '</div>'
        ];
    }

}

This returns ( if I do console.log(structures.loginStructure) ) just a function(). Is there a way I can make it to return the actual array I have in there ?

这个返回(如果我做了console.log(structures.loginStructure))只是一个函数()。有没有一种方法可以让它返回我在那里的实际数组?

The purpose of it would be to have multiple arrays defined as object keys, if it is possible to return such a thing, because it seems easier. Or is there a better way to do this ?

如果可能的话,它的目的是将多个数组定义为对象键,因为它看起来更简单。或者有更好的方法吗?

1 个解决方案

#1


4  

You want:

你想要的:

structures.loginStructure();

structures.loginStructure is simply just returning a reference to the function, instead of executing the function and returning its result. Add the () to the end of it to execute it and return its result.

结构。loginStructure只是返回对函数的引用,而不是执行函数并返回结果。将()添加到末尾以执行它并返回结果。

Alternatively, and maybe better, don't write it as a function. Just declare loginStructure: ['<form name.... Basically, just remove function(){return structure =. A significant difference to note here is that any values of opts would be evaluated immediately, instead of deferred until later when then function would be executed - so please don't blindly update your code to this.

或者,也许更好,不要把它写成函数。刚刚宣布loginStructure:[' <表单名称....基本上,只需删除function(){return structure="。这里需要注意的一个重要的区别是,任何opts的值都将立即被评估,而不是延迟到稍后才会执行,所以请不要盲目地更新你的代码。

#1


4  

You want:

你想要的:

structures.loginStructure();

structures.loginStructure is simply just returning a reference to the function, instead of executing the function and returning its result. Add the () to the end of it to execute it and return its result.

结构。loginStructure只是返回对函数的引用,而不是执行函数并返回结果。将()添加到末尾以执行它并返回结果。

Alternatively, and maybe better, don't write it as a function. Just declare loginStructure: ['<form name.... Basically, just remove function(){return structure =. A significant difference to note here is that any values of opts would be evaluated immediately, instead of deferred until later when then function would be executed - so please don't blindly update your code to this.

或者,也许更好,不要把它写成函数。刚刚宣布loginStructure:[' <表单名称....基本上,只需删除function(){return structure="。这里需要注意的一个重要的区别是,任何opts的值都将立即被评估,而不是延迟到稍后才会执行,所以请不要盲目地更新你的代码。