search
HomeWeb Front-endJS TutorialIntroduction to the difference between javascript function declaration and function expression_javascript skills

Still the same, start with the code:

Copy the code The code is as follows:

<script> <BR>var f = function g() { <BR>return 1; <BR>}; <BR>if (false) { <BR>f = function g(){ <BR>return 2; <BR>}; <BR>} <BR>alert(g()); // 2 <BR></script>

Put this code into IE 6 and it will be completely different from Chrome. Two effects.

The output 2 here is the effect in ie6. If it is in Chrome, it will appear that g is not defined.

This can be considered a JScript bug.

It is obvious here that what is here is just the function expression that defines g. Included in the if conditional statement, only the function expression is defined, and the function is not declared.

Then such direct access will definitely be wrong.

So what is a statement and what is a function expression?

In ECMAScript, the two most common ways to create functions are function expressions and function declarations. The difference between the two is a bit confusing, because the ECMA specification only makes one point clear: function declarations must be marked (Identifier) ​​(which is what everyone often calls the function name), and the function expression can omit this identifier:
Function declaration:
 function function name (parameters: optional) {function body}
Function expression:
Function function name (optional) (parameter: optional) {function body}

So, it can be seen that if the function name is not declared, it must be an expression, but if If a function name is declared, how to determine whether it is a function declaration or a function expression? ECMAScript differentiates by context. If function foo(){} is part of an assignment expression, it is a function expression. If function foo(){} is contained within a function body, or is located in the program At the top, it's a function declaration.

There is also a less common function expression, which is the one enclosed by parentheses (function foo(){}). The reason why it is an expression is because the parentheses () are a grouping operator. It can only contain expressions inside.

You may think that when using eval to execute JSON, the JSON string is usually enclosed in parentheses: eval('(' json ')'). The reason for this is because The grouping operator, that is, this pair of parentheses, will force the parser to parse the JSON curly braces into expressions instead of code blocks.
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C++ 函数声明中的默认参数:全面解析其声明和用法C++ 函数声明中的默认参数:全面解析其声明和用法May 02, 2024 pm 03:09 PM

C++中的默认参数提供对函数参数指定默认值的功能,从而增强代码可读性、简洁性和灵活性。声明默认参数:在函数声明中将参数后加上"="符号,后跟默认值。用法:函数调用时,若未提供可选参数,则会使用默认值。实战案例:计算两个数之和的函数,一个参数必填,另一个可填并有默认值0。优点:增强可读性、增加灵活性、减少样板代码。注意事项:只能在声明中指定,必须位于末尾,类型必须兼容。

C++ 函数的声明和定义的顺序有什么影响?C++ 函数的声明和定义的顺序有什么影响?Apr 19, 2024 pm 01:42 PM

在C++中,函数声明和定义的顺序影响编译和链接过程。最常见的是声明在前,定义在后;也可使用“forwarddeclaration”将定义放在声明前;如果两者同时存在,编译器将忽略声明,仅使用定义。

C++ 函数声明和定义有什么区别?C++ 函数声明和定义有什么区别?Apr 18, 2024 pm 04:03 PM

函数声明告知编译器函数的存在,不包含实现,用于类型检查。函数定义提供实际实现,包含函数体。区分的关键特征包括:目的、位置、作用。理解差异对于编写有效且可维护的C++代码至关重要。

C++ 函数的声明和定义C++ 函数的声明和定义Apr 11, 2024 pm 01:27 PM

函数声明和定义在C++中是必要的,函数声明指定函数的返回类型、名称和参数,而函数定义包含函数体和实现。首先声明函数,然后在程序中使用它并传递所需的参数。使用return语句从函数中返回一个值。

C++编译错误:函数调用与函数声明不符,应该怎样解决?C++编译错误:函数调用与函数声明不符,应该怎样解决?Aug 22, 2023 pm 12:39 PM

C++编译错误:函数调用与函数声明不符,应该怎样解决?在开发C++程序时,难免会遇到一些编译错误,其中之一常见的错误是函数调用与函数声明不符的错误。这种错误广泛存在于C++程序员中,由于不注意函数声明的正确性,导致编译问题,最终浪费时间和精力修复问题,影响开发效率。避免这种错误的方法需要遵循一些规范和标准实践,下面让我们来了解一下。什么是函数调用与函数声明不

C++ 函数声明中的[[nodiscard]]:揭开忽略返回值后果的神秘面纱C++ 函数声明中的[[nodiscard]]:揭开忽略返回值后果的神秘面纱May 01, 2024 pm 06:18 PM

[[nodiscard]]属性指示函数的返回值不得忽略,否则将导致编译器警告或错误,以防止以下后果:未初始化异常、内存泄漏和错误的计算结果。

C++ 函数声明的详细语法:从语法解析到规范用法解析C++ 函数声明的详细语法:从语法解析到规范用法解析Apr 30, 2024 pm 02:54 PM

C++函数声明语法为:returnTypefunctionName(parameterType1parameterName1,...,parameterTypeNparameterNameN);,其中returnType为返回类型,functionName为函数名,parameterType为参数类型,parameterName为参数名,必须以分号结尾。

C++ 函数声明的逐步指南:涵盖每个步骤的详细说明C++ 函数声明的逐步指南:涵盖每个步骤的详细说明May 02, 2024 pm 04:33 PM

函数声明告诉编译器函数的存在,无需提供函数体。步骤如下:指定函数返回类型(void如果无返回值)定义函数名声明函数参数(可选,包括数据类型和标识符)加分号

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!