函数调用时要使用的JavaScript函数返回对象
问题描述:
我有一个像下面这样的函数,但调用这个函数的函数返回undefined,我不知道为什么。函数调用时要使用的JavaScript函数返回对象
$.fileReader_g = function(file){
var reader = new FileReader();
reader.onloadend = function(){
return {
result: reader.result,
size: file.size,
name: file.name
}
}
if (file){
reader.readAsDataURL(file);
} else {
console.log('Upload Failed');
}
}
上述内容由以下方面调用。
$(document).on('change', '.album_art_select', function(){
var cow = $.fileReader_g($(this)[0].files[0]);
console.log(cow);
});
我在哪里错过了?
答
您定义了两个函数。
你指定给$.fileReader_g
的一个。它没有return
声明并且将始终返回undefined
。
另一个您分配给reader.onloadend
。它确实有一个return
声明,但没有什么会注意它。请参阅How to return the response from an asynchronous call?。
你没有从'$ .fileReader_g'函数中'返回'任何东西。该函数不返回任何内容。 –