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

查看:39
本文介绍了从javascript函数返回`undefined`还是`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;
}

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

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

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

调用这个函数的代码知道如何处理 undefined(除非出现可怕的错误,否则它实际上永远不会真正发生)

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

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

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.

Mozilla Docs 没有回答我的问题......谷歌也没有:

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.

例如:

  • [[原型]]

  • [[Prototype]]

普通对象有一个 [[Prototype]] 内部槽,它决定了它们从哪个其他对象继承.当然,必须有一种方法可以说一个对象不从任何其他对象继承.在这种情况下,没有这样的对象"使用 null 表示.

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

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

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

期望返回具有给定 ID 的元素.但是,可能没有具有该 ID 的元素.在这种情况下,没有这样的元素"使用 null 表示.

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天全站免登陆