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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:04:36  来源:igfitidea点击:

javascript function returning undefined

javascriptfunctionreturnundefined

提问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:

笔记:

  1. The JSON is from the Joomla(3.1) tags table.
  2. I am able to retrieve the data.
  3. The val === tagId conditional is working correctly.
  4. I like popcorn.
  1. JSON 来自 Joomla(3.1) 标签表。
  2. 我能够检索数据。
  3. val === tagId 条件工作正常。
  4. 我喜欢爆米花。

回答by Michael Banzon

The call to getJSONcalls your callback function asynchronously.

调用getJSON异步调用您的回调函数。

If you need the function getTagNamesand 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 successhandler of getJSONwhich, first, does not expect any return value, and second, happens asynchronously - way after your getTagNamesfinished executing.

语句return tagArray;返回success处理程序的getJSON结果,首先,不期望任何返回值,其次,异步发生 -getTagNames完成执行后的方式。