Detailed explanation of JavaScript function types_javascript skills
This article mainly introduces ordinary functions, anonymous functions, and closure functions
Table of Contents
- Ordinary functions: Introduces the characteristics of ordinary functions: overrides with the same name, arguments objects, default return values, etc.
- Anonymous functions: Introducing the characteristics of anonymous functions: variable anonymous functions, nameless anonymous functions.
- Closure function: Introduces the characteristics of closure function.
1. Ordinary functions
1.1 Example
function ShowName(name) { alert(name); }
1.2 Overwriting of functions with the same name in Js
In JS, functions are not overloaded. If you define functions with the same function name and different parameter signatures, the later functions will overwrite the previous functions. When called, only the following functions will be called.
var n1 = 1; function add(value1) { return n1 + 1; } alert(add(n1));//调用的是下面的函数,输出:3 function add(value1, value2) { return value1 + 2; } alert(add(n1));//输出:3
1.3 arguments object
arguments is similar to C#'s params, operating variable parameters: the number of parameters passed into the function is greater than the number of parameters when defined.
function showNames(name) { alert(name);//张三 for (var i = 0; i < arguments.length; i++) { alert(arguments[i]);//张三、李四、王五 } } showNames('张三','李四','王五');
1.4 Default return value of function
If the function does not specify a return value, the default return value is 'undefined'
function showMsg() { } alert(showMsg());//输出:undefined
2. Anonymous function
2.1 Variable anonymous function
2.1.1 Description
Functions can be assigned to variables and events.
2.1.2 Example
//变量匿名函数,左侧可以为变量、事件等 var anonymousNormal = function (p1, p2) { alert(p1+p2); } anonymousNormal(3,6);//输出9
2.1.3 Applicable Scenarios
①Avoid function name pollution. If you first declare a function with a name and then assign it to a variable or event, you will abuse the function name.
2.2 Nameless anonymous function
2.2.1 Description
That is, when the function is declared, it is followed by the parameters. When JS syntax parses this function, the code inside is executed immediately.
2.2.2 Example
(function (p1) { alert(p1); })(1);
2.2.3 Applicable Scenarios
①It only needs to be executed once. If the browser is loaded, the function only needs to be executed once and will not be executed later.
3. Closure function
3.1 Description
Assume that function A declares a function B inside, function B refers to a variable outside function B, and the return value of function A is a reference to function B. Then function B is a closure function.
3.2 Example
3.2.1 Example 1: Global reference and local reference
function funA() { var i = 0; function funB() { //闭包函数funB i++; alert(i) } return funB; } var allShowA = funA(); //全局变量引用:累加输出1,2,3,4等 function partShowA() { var showa = funA();//局部变量引用:只输出1 showa(); }
allShowA is a global variable that references function funA. Repeatedly running allShowA() will output the accumulated values of 1, 2, 3, 4, etc.
Execute the function partShowA(), because only the local variable showa is declared internally to reference funA. After execution, due to the scope, the resources occupied by showa are released.
The key to closure is scope: the resources occupied by global variables will only be released when the page changes or the browser is closed. When var allShowA = funA(), it is equivalent to allShowA referencing funB(), so that the resources in funB() will not be recycled by GC, so the resources in funA() will not be recycled either.
3.2.2 Example 2: Parametric closure function
function funA(arg1,arg2) { var i = 0; function funB(step) { i = i + step; alert(i) } return funB; } var allShowA = funA(2, 3); //调用的是funA arg1=2,arg2=3 allShowA(1);//调用的是funB step=1,输出 1 allShowA(3);//调用的是funB setp=3,输出 4
3.2.3 Example 3: Variable sharing within parent function funA
function funA() { var i = 0; function funB() { i++; alert(i) } allShowC = function () {// allShowC引用匿名函数,与funB共享变量i i++; alert(i) } return funB; } var allShowA = funA(); var allShowB = funA();//allShowB引用了funA,allShowC在内部重新进行了绑定,与allShowB共享变量i
3.3 Applicable Scenarios
① Ensure the safety of the variables inside the function funA, because the variables of funA cannot be directly accessed from the outside.
The above is the entire content of this article, I hope it will be helpful to everyone’s study.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft