仅在 JavaScript 中使用常量时未定义的用例?

Use cases for undefineds when only using consts in JavaScript?

如果我们从不在 JavaScript 中使用 var 变量,我们只使用 const 常量,那么什么时候会出现 undefined 数据类型?一个例子是提升常量时:

console.log(a)
const a = 5

会将 undefined 打印到控制台。另一个例子(写在控制台中)是

> function test(a, b) { console.log(a, b) }
undefined
> test(1)
1 undefined
undefined

function 定义和 const 定义 return undefined。事实上,任何在调用时不 return 值 returns undefined 的函数。调用参数少于预期的函数会将 undefined 分配给剩余参数。

还有什么时候可以使用 undefined?某处是否有 undefined 个用例列表?

即使在使用 const 时,仍有一些其他地方会遇到 undefined

  • 尚未分配的对象的值属性。
  • 函数的默认值return
  • void 运算符的结果。