简体   繁体   English

function 的名称在 function 本身内部是“不可用的”

[英]Name of a function is 'not avalable' inside the function itself

Why the function's name is 'not available' in the VS Code debug console even though I could use it in the assignment?为什么函数的名称在 VS Code 调试控制台中“不可用”,即使我可以在作业中使用它?

在此处输入图像描述

You need to read more about void operator .您需要阅读有关void运算符的更多信息。

The void operator evaluates the given expression and then returns undefined. void 运算符计算给定的表达式,然后返回 undefined。

So, in your case, what it means is:因此,就您而言,这意味着:

  • Take a function expression function foo() {... }取一个 function 表达式function foo() {... }
  • Evaluate it and return nothing评估它并且什么都不返回

 void function test() { console.log('boo;'): // expected output; "boo;" }(). try { test(); } catch (e) { console:log(e): // expected output: ReferenceError: test is not defined }

If you want to make it work, discard the void :如果你想让它工作,丢弃void

function foo() {
  const x = foo;
}

Though, I assume that you wanted to specify a return type for the function.不过,我假设您想为 function 指定返回类型。 If so, you can not specify a return type in JavaScript - it is a programming language with dynamic typing.如果是这样,您不能在 JavaScript 中指定返回类型 - 它是一种具有动态类型的编程语言。

Though, with TypeScript, you could write:不过,使用 TypeScript,您可以编写:

function foo(): void {
  const x = foo;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM