在第 464 和 465 页的“Head First Javascript”一书中,有两个相互矛盾的陈述。
第 464 页
When the browser evaluates a function declaration, it creates a function as well as a variable with the same name as the function, and stores the function reference in the variable.
第 465 页
A function declaration doesn't return a reference to a function; rather it creates a variable with the name of the function and assigns the new function to it.
那是哪一个呢?当解释器查看函数声明时,会创建一个变量 - 它是将一个函数还是一个函数引用分配给该变量?
“函数声明不返回对函数的引用”这句话是错误的吗?
最佳答案
JavaScript 并没有真正区分这两者。
任何时候你有一个值是一个对象,这个值实际上是对该对象的引用。
(请记住,函数只是 JS 中的一种对象)。
函数声明不能返回任何东西,它们不能存在于任何有返回左侧的地方(如果有,那么它们就是函数表达式而不是声明)。他们在当前范围内创建一个与他们同名的变量。
关于javascript - JavaScript 函数声明是否返回对函数的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39934764/