【发布时间】:2019-10-09 01:44:41
【问题描述】:
我是 JS 新手,如果这听起来很愚蠢,请原谅我。我在玩函数声明和函数表达式的概念。
我有以下代码:
var printSomething = function printSomeString(string) {
console.log(string);
}
console.log(typeof printSomething); // function
console.log(typeof printSomeString); // undefined
如果我按照 JavaScript 中 hoisting 的定义,当我使用 printSomething 和 printSomeString 时,它们应该可用,因为它们的声明已被提升。
typeof printSomething 返回函数,但 typeof printSomeString 返回未定义。为什么这样?
这个命名函数表达式在使用之前不是已经声明和提升了吗?
命名函数表达式本身不是函数吗?
另外,当我调用printSomeString('Some STRING') 时,它会返回以下内容
Uncaught ReferenceError: printSomeString is not defined
这是怎么回事?
【问题讨论】:
-
printSomeString只存在于它自己的范围内,如果可以的话:jsfiddle.net/vqL2814y
标签: javascript hoisting function-declaration function-expression