javascript - JavaScript 函数声明是否返回对函数的引用?

标签 javascript html web

在第 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/

相关文章:

javascript - 带有超时功能的 Google 标记掉落动画

Firefox 的 geo.wifi.url 的 https ://www. google.com/loc/json 没用?

android - 用户登录直接从应用程序或从应用程序到 Web 界面写入数据库?

javascript - <button onclick=...> 和 <a href ="#"onclick=.../> 有什么区别

javascript - 使用 Typeahead 自动完成时出现问题

javascript - 哪种是在回合制多人游戏中实现计时器的最佳方法?

html - 最后一个表格列更宽

javascript - 删除 HTML 标签并考虑文本区域中的换行符

javascript - 放大 - 缩小 Javascript 背景上的问题

macos - 将文件传输到 Web 服务器时转换 Unicode 分解