最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

using multiple return statements in JavaScript - Stack Overflow

programmeradmin9浏览0评论

I am trying to use multiple returns but just keep breaking the code. I have tried a few examples, but cant find the right combination.

How can I combine these two return statements into one?

$(".bar").popover({
content: 
    function (){
        return $(this).data('dataObj').status;
        return $(this).data('dataObj').timeline;
    }
});

I am trying to use multiple returns but just keep breaking the code. I have tried a few examples, but cant find the right combination.

How can I combine these two return statements into one?

$(".bar").popover({
content: 
    function (){
        return $(this).data('dataObj').status;
        return $(this).data('dataObj').timeline;
    }
});
Share Improve this question asked Mar 14, 2013 at 14:00 JB.JB. 9133 gold badges16 silver badges29 bronze badges 2
  • Doesn't popover content attribute require a string? – ATOzTOA Commented Mar 14, 2013 at 14:04
  • just use return $(this).data('dataObj') – zgr024 Commented Mar 14, 2013 at 14:15
Add a comment  | 

4 Answers 4

Reset to default 11

Use

function (){
    return $(this).data('dataObj');
}

OR

function (){
    // return an array
    return [ $(this).data('dataObj').status, $(this).data('dataObj').timeline ]
}

OR

function (){
    // return a associative array
    return { "status": $(this).data('dataObj').status, "timeline": $(this).data('dataObj').timeline }
}

And process the components in the caller.

Update

The content parameter for popover needs a string as argument, you can do this:

function (){
    return $(this).data('dataObj').status + " " + $(this).data('dataObj').timeline;
}

Putting aside this specific case, where the plugin demands a certain type of return value (apparently a string in this case), you can't really... A return statement terminates the function. What you'll have to do is return an object (or an array) containing those two values -

var status = $(this).data('dataObj').status;
var timeline = $(this).data('dataObj').timeline;
return [status,timeline];

Or

var status = $(this).data('dataObj').status;
var timeline = $(this).data('dataObj').timeline;
var returnObj = {'status':status, 'timeline':timeline};
return returnObj;

You can return objext ir array containig those two items

$(".bar").popover({
content: 
    function (){
        return 
        {
        status: $(this).data('dataObj').status;
        timeline: $(this).data('dataObj').timeline;
        }
    }
});

Try returning an array with .status and .timeline as elements. Ok Lix was faster.

发布评论

评论列表(0)

  1. 暂无评论