在 JavaScript 中使用多个 return 语句
问题描述:
我正在尝试使用多次返回,但只是不断破坏代码.我尝试了几个例子,但找不到正确的组合.
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;
}
});
答
使用
function (){
return $(this).data('dataObj');
}
或
function (){
// return an array
return [ $(this).data('dataObj').status, $(this).data('dataObj').timeline ]
}
或
function (){
// return a associative array
return { "status": $(this).data('dataObj').status, "timeline": $(this).data('dataObj').timeline }
}
并处理调用者中的组件.
And process the components in the caller.
更新
popover
的 content
参数需要一个字符串作为参数,你可以这样做:
The content
parameter for popover
needs a string as argument, you can do this:
function (){
return $(this).data('dataObj').status + " " + $(this).data('dataObj').timeline;
}