In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of JavaScript anonymous function knowledge points, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this JavaScript anonymous function knowledge point. Let's take a look at it.
I. the scope of variables
Think about it: can you use a variable anywhere after you declare it?
Answer: no.
For example: variables declared by the var keyword within a function cannot be accessed outside the function.
Summary: the use of variables is scoped.
Scope division: global scope, function scope, and block-level scope (provided by ES6).
Variables corresponding to different domains: global variables, local variables, block-level variables (provided by ES6).
Global variables: variables that are not declared within any function (display definition) or omitted from var declaration variables (implicitly defined) are called global variables.
Scope: it can be used in all scripts in the same page file.
Local variable: a variable defined in the body of a function using the var keyword is called a local variable, and it is valid only in the body of the function.
Block-level variables: variables declared by the let keyword provided by ES6 are called block-level variables and are valid only in the middle of "{}", such as if, for, or while statements.
Garbage collection mechanism
In JavaScript, local variables exist only during the execution of a function, and during this process, local variables are allocated space on memory (stack or heap) to store their values, and then these variables are used in the function until the function ends. Once the function execution is finished, there is no need for local variables, and JavaScript will automatically release the memory space they occupy through the garbage collection mechanism.
To retain the values of local variables in development, you can do so in the following two ways:
Second, anonymous function
Concept: the so-called function expression refers to assigning the declared function to a variable and completing the function call and parameter transfer through the variable. It is also another way to implement the custom function in JavaScript.
Function expression
Anonymous function
Concept: anonymous functions refer to functions that do not have a function name.
Function: can effectively avoid the pollution of global variables and the conflict of function names.
Description: it is not only another representation of function expression, but also can be called by function declaration.
Arrowhead function
Concept: a new syntax has been introduced in ES6 to write anonymous functions, which we call arrow functions.
Features: the syntax of an arrow function expression is shorter than that of a function expression.
Nesting and recursive function nesting and scope chain
What is a nested function: a declaration that there is another function within one function.
Features: the inner function can only be executed in the scope of the outer function. During the execution of the inner function, if a variable needs to be introduced, it will first be found in the current scope. If it is not found, it will continue to look up the scope until the global scope. We call this chained query relationship the scope chain.
Recursive call
Concept: a recursive call is a special call in a function nested call. It refers to the process by which a function calls itself in its body, which is called a recursive function.
Let's take calculating factorial as an example to demonstrate.
Be careful
Although recursive call is very suitable for traversing multi-dimensional arrays with unfixed dimensions, it takes up a lot of memory and resources, and it is difficult to implement and maintain, so recursive calls of functions should be used cautiously in development.
Case
Find the value of term N of Fibonacci series
Understand what is the Fibonacci series
Fibonacci series is also called golden section series, such as "1, 1, 2, 3, 5, 8, 13, 21 …" .
Find the rule: this series starts with the third term, and each term is equal to the sum of the first two terms.
Code realization idea
Less than 0, an error message is given.
Equals 0 and returns 0.
Equals 1, returns 1.
Greater than 1, according to the law found and using function recursive calls to achieve.
This is the end of the article on "what are the knowledge points of JavaScript anonymous functions?" Thank you for reading! I believe you all have a certain understanding of "what are the knowledge points of JavaScript anonymous functions". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.