从 javascript 函数返回 `undefined` 或 `null` 更好吗?

Is it better to return `undefined` or `null` from a javascript function?(从 javascript 函数返回 `undefined` 或 `null` 更好吗?)

本文介绍了从 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 文档没有回答我的问题......谷歌也没有:

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` 更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从 javascript 函数返回 `undefined` 或 `null` 更好吗?

基础教程推荐