search
HomeWeb Front-endJS TutorialFunction declaration and function expression in Javascript (wonderful and cunning techniques)_javascript skills

Give an example:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute it
]

You will know this after trying it The meaning of the code is to declare a function and then execute it immediately. Because the variable scope in Javascript is based on functions, this can avoid variable pollution, but the bit operator "~" here is confusing at first glance. If you remove it and run again, an error will be reported: SyntaxError.

Before explaining why, let us first clarify two concepts in Javascript: function declaration and function expression:

First, let’s take a look at what a function declaration is:

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
Let’s see what it looks like is a function expression:
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

Now look back at the beginning of the article The question is why an error will be reported after removing the bit operator "~". This is because from the perspective of syntax analysis, Javascript does not allow the use of parentheses directly after the function declaration, while function expressions do not have this restriction. Adding a "~" operator in front of a function declaration allows the syntax parser to treat the following as a function expression. Similarly, adding "!, , -" and other operators in front of a function declaration is also feasible.
Then why don’t we use the following function expression?

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

Although from the perspective of syntax analysis There seems to be no problem, but the above code has drawbacks. It introduces a variable, which may pollute the existing operating environment and cause potential problems.


<script> ~function() { alert("hello, world."); }(); </script>[Ctrl A Select all Note: <script> function() { alert("hello, world."); }; function foo() { alert("hello, world."); }; </script>If you need to introduce external Js, you need to refresh to execute it <script> var foo = function() { alert("hello, world."); }; </script>]<script> var foo = function() { alert("hello, world."); }(); </script> <script> (function() { alert("hello, world."); })(); </script>I understand the principle, no matter No matter what writing method you encounter, Monk Zhanger will no longer be confused.
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
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool