using multiple return statements in JavaScript

前端 未结 4 1727
忘掉有多难
忘掉有多难 2021-01-16 12:26

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 r

相关标签:
4条回答
  • 2021-01-16 12:46

    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;
    }
    
    0 讨论(0)
  • 2021-01-16 12:46

    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;
            }
        }
    });
    
    0 讨论(0)
  • 2021-01-16 12:50

    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;
    
    0 讨论(0)
  • 2021-01-16 12:55

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

    0 讨论(0)
提交回复
热议问题