简体   繁体   English

返回函数内的对象

[英]returning an object inside a function

I would like to not have to write additional functions to populate the areas_of_interest variable, can I do it like so? 我想不必编写其他函数来填充areas_of_interest变量,我可以这样做吗?

function prepareResults() {

    var area_of_interest = $('.checkboxes-wr').find("li");

    if (area_of_interest.length) {

        $.each(area_of_interest, function(x){

            results.items[x] = {
                'id' : $(this).attr("id"),
                'title' : $(this).find("h6").text().trim(),
                'areas_of_interest' : function() {
                    return {'test' : 'test'};
                },
                'potential_treatments' : {}
            }


        });

    }

}

this worked: 这工作:

function prepareResults() { function prepareResults(){

    var area_of_interest = $('.checkboxes-wr').find("li");

    if (area_of_interest.length) {

        $.each(area_of_interest, function(x){

            var interests = function(x) {
                return {'test' : x}
            }

            results.items[x] = {
                'id' : $(this).attr("id"),
                'title' : $(this).find("h6").text().trim(),
                'areas_of_interest' : interests(x),
                'potential_treatments' : {}
            }


        });

    }

}

thanks to everyone who helped in the comments above. 感谢所有在上述评论中提供帮助的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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