Javascript 函数总是返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17976676/
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 10:18:17  来源:igfitidea点击:

Javascript function always returns null

javascript

提问by Jetson John

I am newbie to the JavaScript world. I have a doubt how the return statement works in JavaScript.

我是 JavaScript 世界的新手。我怀疑返回语句在 JavaScript 中的工作方式。

What I am trying to do is to have a function pass one arguments paramand looking whether the parammatch the key of the exampleDataobject. If match found I want to return the value and break the looping of each function also break the function I don't want to execute any other statement under the each function. If no match found the function must return null. But currently the function always return null value.

我想要做的是让函数传递一个参数param并查看是否param匹配exampleData对象的键。如果找到匹配,我想返回值并中断每个函数的循环也中断函数我不想在每个函数下执行任何其他语句。如果未找到匹配项,则该函数必须返回 null。但目前该函数总是返回空值。

function exampleFunction(param){
    $.each(exampleData, function (key, value) {
        if(key == param){
        return value;
        }
    });
    return null;
}

Any insight into this would highly be appreciated. Thank you.

对此的任何见解将不胜感激。谢谢你。

回答by canon

Your example doesn't seem to need a loop. That and returning from the loop-delegate doesn't do anything for you.

您的示例似乎不需要循环。那和从循环委托返回对您没有任何作用。

You can test if a key appears in an object using the inoperator.

您可以使用in运算符测试键是否出现在对象中。

function exampleFunction(param){
  return param in exampleData ? exampleData[param] : null;
}

回答by C???

Given Crazy Train's comment, you need to capture the matching value so you can return it later:

鉴于 Crazy Train 的评论,您需要捕获匹配值以便稍后返回:

function exampleFunction(param) {
    var match = null;
    $.each(exampleData, function (key, value) {
        if (key == param) {
            match = value;
            return false; // to end the $.each early
        }
    });
    return match;
}

回答by Douglas Barbin

Try this:

试试这个:

function exampleFunction(param){ 
    return exampleData[param];
  }

EDIT: If you want to return null if the parameter is undefined, then take a look at Paul and Crazy Train's comments:

编辑:如果您想在未定义参数的情况下返回 null,请查看 Paul 和 Crazy Train 的评论:

return (typeof exampleData[param] === 'undefined')? null: exampleData[param]; 

OR

或者

return (param in exampleData) ? exampleData[param] : null;

回答by Douglas Barbin

There are a few issues with your approach:

你的方法有几个问题:

  1. The inner function you defined is returning to $.each, not to the outer function, so you never get that value.
  2. You don't need to iterate over the object in the wild way you are. Objects in Javascript are hash tables, you can simply do the following
  1. 您定义的内部函数是returning to $.each,而不是外部函数,因此您永远不会获得该值。
  2. 您不需要以您的狂野方式迭代对象。Javascript 中的对象是哈希表,您可以简单地执行以下操作

:

function exampleFunction(param) {
    return exampleData[param];
}

If exampleData[param]is undefined, it will return undefined, otherwise, it will return the value stored there.

如果exampleData[param]undefined,它将返回undefined,否则,它将返回存储在那里的值。

If you absolutely need the undefined case to return nullinstead of undefined, then it becomes a bit more complicated.

如果您绝对需要返回 undefined casenull而不是undefined,那么它会变得有点复杂。

function exampleFunction(param) {
    if(exampleData.hasOwnProperty(param) {
        return exampleData[param];
    } else {
        return null;
    }
}