JQuery将函数返回给变量

时间:2014-06-03 14:58:33

标签: javascript jquery

有没有办法将变量设置为等于函数,但是变量等于函数,它等于函数的返回值?

对此进行了一些搜索但没有取得任何成功,很可能是因为我不知道如何表达这个问题,但是我们非常感谢任何帮助,这就是我所拥有的:

我不需要在函数之后设置var ztype = zoom(),而是在没有额外的代码行的情况下将返回值分配给zoom吗?

var zoom = function(){ //can be set to Full / percentage / Image / left empty for native image size
    var type = $('.carousel').attr('data-zoom');
    if(!type){
        type = $(that).attr('data-zoom');
    }

    return type;
}
var ztype = zoom();

1 个答案:

答案 0 :(得分:7)

如果我正确理解你,你试图将变量设置为函数的结果。这应该做你所希望的;

var ztype = function(){ //can be set to Full / percentage / Image / left empty for native image size
    var type = $('.carousel').attr('data-zoom');
    if(!type){
        type = $(that).attr('data-zoom');
    }

    return type;
}();