简体   繁体   中英

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?

在此处输入图像描述

You need to read more about void operator .

The void operator evaluates the given expression and then returns undefined.

So, in your case, what it means is:

  • Take a function expression 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 :

function foo() {
  const x = foo;
}

Though, I assume that you wanted to specify a return type for the function. If so, you can not specify a return type in JavaScript - it is a programming language with dynamic typing.

Though, with TypeScript, you could write:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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