Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery return a function to a variable

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 :

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();
like image 801
Adjit Avatar asked Feb 13 '23 19:02

Adjit


1 Answers

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;
}();
like image 178
Pudge601 Avatar answered Feb 15 '23 10:02

Pudge601