1

我是 JS 的新手,所以如果这听起来很愚蠢,请原谅我。我在玩函数声明函数表达式的概念。

我有以下代码:

var printSomething = function printSomeString(string) {
  console.log(string);
}

console.log(typeof printSomething); // function
console.log(typeof printSomeString); // undefined

如果我hoisting按照 JavaScript 中的定义,当我使用printSomethingandprintSomeString时,它们应该是可用的,因为它们的声明已被提升。

typeof printSomething 返回函数,但typeof printSomeString返回未定义。为什么这样?

这个命名函数表达式在使用之前不是已经声明和提升了吗?

命名函数表达式本身不是函数吗?

另外,当我打电话时printSomeString('Some STRING'),它会返回以下内容

未捕获的 ReferenceError:未定义 printSomeString

这里发生了什么?

4

1 回答 1

1

printSomeString是一个不是全局变量,它的局部变量是另一个函数printSomething。尝试console.log()在里面使用。

var printSomething = function printSomeString(string) {
  console.log(typeof printSomeString)
}

console.log(typeof printSomething); // function
printSomething()

于 2019-05-22T14:25:55.483 回答