简体   繁体   English

函数返回'undefined'值

[英]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 . 它必须是1 Where am I missing? 我在哪里错过了?

Function myid_templates_editor_image_id_generator is returning nothing. 函数myid_templates_editor_image_id_generator什么都没有返回。 The only return statement you have ( return a; ) exits function($){...} but it's not propagated to outer function. 你拥有的唯一return语句( return a; )退出function($){...}但它不会传播到外部函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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