Javascript 函数返回';未定义';价值
我有以下代码:Javascript 函数返回';未定义';价值,javascript,function,return-value,alert,Javascript,Function,Return Value,Alert,我有以下代码: 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){
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);
}
它发出两次警报:
为什么返回值为“未定义”?它必须是
1
。我在哪里丢失了?函数myid\u模板\u编辑器\u图像\u id\u生成器
没有返回任何内容。您拥有的唯一返回语句(returna;
)退出函数($){…}
,但它不会传播到外部函数。,因为您的函数myid\u模板编辑器\u图像\u id\u生成器
不会返回任何内容。尝试在其主体中的一个表达式前面添加return
关键字。Javascript没有隐式返回。因为您没有返回myid\u模板\u编辑器\u图像\u id\u生成器
范围内的任何内容。