javascript javascript函数返回未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17371982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript function returning undefined
提问by jremydeaton
I am sure I am doing this all kinds of wrong, but I have the following function returning 'undefined' in the console even though it can console.log() the wanted values from the same place in the function as commented in the code.
我确信我做的这一切都是错误的,但我有以下函数在控制台中返回“未定义”,即使它可以 console.log() 来自函数中相同位置的所需值,如代码中所注释。
var tags = [4, 5];
console.log(getTagNames(tags)); // 'undefined'
function getTagNames(tagArray) {
$.getJSON('js/tags.json', function(data) {
for (var i in tagArray) {
tagArray[i] = tagArray[i].toString();
var val = tagArray[i];
for (var t in data) {
var tag = data[t];
var tagName = tag.alias;
var tagId = tag.id;
if (val === tagId) {
tagArray[i] = tagName;
}
};
}
console.log(tagArray); // output ["foo", "bar"]
return tagArray;
});
}
The other odd thing is that, after running this code in the browser, I can type "tags" into the browser console and it gives me the correct result ["foo", "bar"]
. However when I try using the tags variable (i.e.: text value for an element and such), it doesn't work...
What Gives? JavaScript isn't my first language so I am a little confused of exactly how it behaves. I just don't understand.
另一个奇怪的事情是,在浏览器中运行此代码后,我可以在浏览器控制台中键入“标签”,它会给出正确的结果["foo", "bar"]
。但是,当我尝试使用标签变量(即:元素的文本值等)时,它不起作用......什么给出?JavaScript 不是我的母语,所以我对它的行为方式有点困惑。我就是不明白。
I have read almost all of the "Questions that may already have my answer", but the answers supplied where those that I couldn't figure out how to apply to my function.
我已经阅读了几乎所有的“可能已经有了我的答案的问题”,但是这些答案提供了那些我无法弄清楚如何应用于我的功能的答案。
NOTE:
笔记:
- The JSON is from the Joomla(3.1) tags table.
- I am able to retrieve the data.
- The val === tagId conditional is working correctly.
- I like popcorn.
- JSON 来自 Joomla(3.1) 标签表。
- 我能够检索数据。
- val === tagId 条件工作正常。
- 我喜欢爆米花。
回答by Michael Banzon
The call to getJSON
calls your callback function asynchronously.
调用getJSON
异步调用您的回调函数。
If you need the function getTagNames
and call it many places in your code you could make changes to make it take a callback function itself:
如果您需要该函数getTagNames
并在代码中的很多地方调用它,您可以进行更改以使其本身采用回调函数:
function getTagNames(tagArray, cb) {
$.getJSON('js/tags.json', function(data) {
for (var i in tagArray) {
tagArray[i] = tagArray[i].toString();
var val = tagArray[i];
for (var t in data) {
var tag = data[t];
var tagName = tag.alias;
var tagId = tag.id;
if (val === tagId) {
tagArray[i] = tagName;
}
};
}
console.log(tagArray); // output ["foo", "bar"]
cb(tagArray);
});
}
and then make calls to it like this:
然后像这样调用它:
getTagNames(tags, function(tagArray) {
// do something with tagArray
});
回答by Igor
Statement return tagArray;
returns result of success
handler of getJSON
which, first, does not expect any return value, and second, happens asynchronously - way after your getTagNames
finished executing.
语句return tagArray;
返回success
处理程序的getJSON
结果,首先,不期望任何返回值,其次,异步发生 -getTagNames
完成执行后的方式。