从javascript函数返回undefined或null是否更好? [英] Is it better to return `undefined` or `null` from a javascript function?

查看:113
本文介绍了从javascript函数返回undefined或null是否更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 函数getNextCard(searchTerms){
//设置一些变量

//做一堆逻辑来选择下一张名为searchTerms的字符串,通过我称之为'searchTerms'的所有逻辑被忽略,因为它不是对我的问题很重要。
// ...

//如果我们找到下一张卡片,比给它
if(nextCardFound)
return nextCardFound;

//否则 - 我返回undefined
return undefined;
}

问题:在这里返回null会更好吗?



我可以通过我想要的任何东西 - 显然......我只是不确定最好用什么。



调用此函数的代码知道如何处理未定义的(实际上,除非出现可怕的错误,否则实际上不会发生)



我问这个问题的原因是,我听说某处听起来像不要将未定义的变量分配给或其他地方 - 它会使调试更加困难。因此,我可以看到 null 被传回的事实告诉我返回正在工作 - 但基本上功能类似于 undefined




文件:



Mozilla文档没有回答我的问题... google也没有:\



这个SO问题 - 对于我想要弄明白的东西太宽泛了。

解决方案

我会说没有最好的方法,甚至标准函数有时会选择其中之一。



例如:


  • [[Prototype]]



    [原型]]内部插槽,它确定从哪个其他对象开始从...开始。当然,必须有一种方法可以说一个对象不会从其他任何一个继承。在这种情况下,没有这样的对象用 null 表示。


  • Object.getOwnPropertyDescriptor



    预期会返回一个属性描述符,即描述属性的对象(例如值,可写性,可枚举性和可配置性)。但是,该财产可能不存在。在这种情况下,没有这样的属性用 undefined 表示。


  • document.getElementById



    预期会返回给定ID的元素。但是,该ID可能没有元素。在这种情况下,没有这样的元素用 null 表示。




因此,只要选择你喜欢的或者认为对你的特定情况更有意义的。


I have a function which I have written which basically looks like this:

function getNextCard(searchTerms) {
  // Setup Some Variables

  // Do a bunch of logic to pick the next card based on termed passed through what I'll call here as 'searchTerms' all of this logic is omitted because it's not important for my question.
  // ...

  // If we find a next card to give, than give it
  if (nextCardFound)
    return nextCardFound;

  // Otherwise - I'm returning undefined
  return undefined;
}

Question: Would it be better to return "null" here?

I can pass whatever I want back - obviously... I just wasn't sure what is the best thing to use.

The code that calls this function knows how to deal with undefined (it actually won't ever really happen unless something goes horribly wrong)

The reason I'm asking this question is that I heard somewhere something that sounded like "Don't assign undefined to variables" or something - that it will make it harder to debug. So, the fact that I can see that null gets passed back tells me that the return is working - but basically function similar to undefined.


Documentation:

Mozilla Docs Didn't answer my question... google didn't either :\

This SO Question - was way too broad for what I'm trying to figure out here.

解决方案

I will argue there is no best way, and even standard functions sometimes choose one or the other.

For example:

  • [[Prototype]]

    Ordinary objects have a [[Prototype]] internal slot, which determines from which other object they inherit from. Of course, there must be a way to say that an object does not inherit from any other one. In this case, "there is no such object" is represented using null.

  • Object.getOwnPropertyDescriptor

    It is expected to return a property descriptor, that is, an object which describes a property (e.g. value, writability, enumerability and configurability). However, the property may not exist. In this case, "there is no such property" is represented using undefined.

  • document.getElementById

    It is expected to return the element with the given ID. However, there might be no element with that ID. In this case, "there is no such element" is represented using null.

So just choose whatever you prefer or think makes more sense for your specific case.

这篇关于从javascript函数返回undefined或null是否更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆