Javascript函数声明和函数表达式

deltamaster posted @ Mar 21, 2014 09:00:02 PM in JavaScript , 4015 阅读

先看一下下面的代码:

var a = function(x) {document.write(x + "\n");}(1); // OK - output "1"
function(x) {document.write(x + "\n");}(1); // Uncaught SyntaxError: Unexpected token (

小伙伴们就议论开了,为什么第一行的程序运行成功,而第二行的程序有语法错误呢?错误信息里提到的“(”是指哪一个括号呢?

之后又诞生了一系列的写法,来分别验证结果:

function f(x){document.write(x + "\n");}(1); // No output
(function(x) {document.write(x + "\n");})(1); // OK - output "1"
(function(x) {document.write(x + "\n");}(1)); // OK - output "1"
+function(x) {document.write(x + "\n");}(1); // OK - output "1"
~function(x) {document.write(x + "\n");}(1); // OK - output "1"

在这里先直接揭晓答案,在语句开头出现function,则被视为函数声明(FunctionDeclaration),否则视为函数表达式(FunctionExpression)。两者都同样构造一个函数对象,但是有两个区别:

  1. 函数声明必须有函数名作为函数标识符,而对于函数表达式则是可选的。(上面报出语法错误的那行代码,实际上是说不允许函数声明不含有标识符)
  2. 函数声明是与语句(Statement)平级,不求值的语法元素,而表达式有返回值。(上面没有输出的那行代码,是因为被视为一句函数声明,还有一句是(1);语句)

为何这样设计?

当解释器进行语法分析时,期待读入一个Statement或FunctionDeclaration,此时读入到词法元素“function”。此时就有了二义性,这里既可能是FunctionDeclaration的第一个词法元素,也可能是ExpressionStatement的第一个语法元素(即其中FunctionExpression的第一个词法元素),为了避免这种二义性,规定在这种情况下,将这个“function”视作FunctionDeclaration的第一个词法元素,于是就有了上面的结论和实验结果。

* 本文在CC BY-SA(署名-相同方式共享)协议下发布。
  • 无匹配
  • 无匹配
Liwovosa 说:
May 05, 2021 03:17:53 PM

Well-Written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing – can't pause to read more posts. Thanks for the precious help. 123movies.com official site

AP SSC Urdu Question 说:
Sep 17, 2022 01:18:14 AM

Urdu is one of the main languages in the state, and this is the first language for Urdu Medium students, there are fewer schools are working in all districts of the state, all the applicable students also can download AP SSC Urdu Model Paper 2023 Pdf in chapter wise for all lessons of the course, AP SSC Urdu Question Paper download, and practice the Ibtedai Question bank to get better rank in all exams conducted by BSEAP. Urdu is one of the main languages in the state, and this is the first language for Urdu Medium students, there are fewer schools are working in all districts of the state, all the applicable students also can download AP SSC Urdu Model Paper 2023 Pdf in chapter wise for all lessons of the course.

reese 说:
Nov 29, 2022 12:13:51 AM

A function declaration is a statement that creates a function. A function is an object with a property named "prototype" that contains the real estate agent East Wenatchee function's code. A function expression is a function literal. It is a piece of code that defines a function. A function expression can be anonymous.

zaiya 说:
Jan 14, 2023 11:28:16 AM

"In JavaScript, there are two ways to create a function: function declarations and function expressions. Function declarations are created with the keyword ""function"": function myFunction() { // do something } Function expressions are created by assigned a function to a variable: var myFunction = function() { // do something } There are advantages and disadvantages to both approaches. Function declarations are hoisted, which means that they are available before they are actually declared in the code. This can be helpful if you want to use a function before it is defined. pure CBD products However, function expressions are not hoisted, so they must be declared before they are used. "

civaget 说:
Jan 11, 2024 11:09:06 PM

This web site doesn’t show up appropriately on my apple iphone – you might wanna try and repair that how to make my google docs dark mode

civaget 说:
Jan 18, 2024 02:58:59 AM

Discover opga's diverse world, from massages to Kiss Room, where connections flourish and individual preferences are celebrated.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter