JQuery将函数返回给变量 [英] JQuery return a function to a variable

查看:89
本文介绍了JQuery将函数返回给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法设置一个等于函数的变量,而不是变量等于函数它等于函数返回值?

Is there any way to set a variable equal to a function, but instead of the variable being equal to the function it is equal to the functions return value?

搜索了一下这一点没有任何成功,很可能是因为我不知道如何表达问题,但任何帮助都表示赞赏,这就是我所拥有的:

Searched for a bit on this without any success, it is most likely because I don't know how to phrase the question, but any help is appreciated, this is what I have :

在函数之后不必设置 var ztype = zoom(),无论如何我都要将返回值赋给 zoom 没有多行代码?

Instead of having to set var ztype = zoom() after the function, is there anyway for me to assign the return value to zoom without an extra line of code?

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();


推荐答案

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

If I'm understanding you correctly, you're trying to set a variable to the result of a function. This should do what you're hoping for;

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;
}();

这篇关于JQuery将函数返回给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆