简体   繁体   中英

Function returns 'undefined' value

I have this code below:

alert('Returned value : ' + myid_templates_editor_image_id_generator());


//Generates unique id for every image created
function myid_templates_editor_image_id_generator(){    
    (function($){
        var a = 1;
        while(true){                    
            if($('#myid_templates_editor_image_' + a).length == 0) {                
                alert('Inside value : ' a);
                return a;               
            }
            a++;                            
        }
    })(jQuery); 
}

It alerts two times:

在此输入图像描述

在此输入图像描述

Why is the return value 'undefined'? It must be 1 . Where am I missing?

Function myid_templates_editor_image_id_generator is returning nothing. The only return statement you have ( return a; ) exits function($){...} but it's not propagated to outer function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM