In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to implement the self-executing function in JavaScript. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Self-executing function (function () {... }) () and (function () {… } ())
The function definition mentioned earlier is separated from the time of calling, and the definition and execution described here are carried out together.
(function () {… }) () and (function () {… } () is a common way for javascript to execute functions immediately. At first, I thought it was an anonymous function wrapped in parentheses, followed by a parenthesis to call the function, and finally achieved the purpose of immediate execution after the function definition. Later, I found that the reason for adding parentheses was not the case. To understand the immediate execution of functions, you need to understand some of the basic concepts of functions.
Function declarations, function expressions, anonymous functions
Function declaration: function fnName () {... Use the function keyword to declare a function, and then specify a function name, called a function declaration.
Function expression var fnName = function () {… Use the function keyword to declare a function without giving it a name, and finally assign an anonymous function to a variable called a function expression, which is the most common form of function expression syntax.
Anonymous function: function () {}; use the function keyword to declare a function, but do not name the function, so it is called anonymous function, anonymous function belongs to function expression, anonymous function has many functions, assigning a variable creates a function, assigning an event becomes an event handler or creating a closure, and so on.
The difference between a function declaration and a function expression is
1. When parsing javascript code, the Javascript engine will 'function declaration' (Function declaration Hoisting) the function declaration on the current execution environment (scope), and the function expression will not parse the function expression line by line from top to bottom until the Javascirtp engine executes to its line.
Second, after the function expression can be immediately called in parentheses, the function declaration can not, can only be called in the form of fnName (). Here are two examples of the difference between the two.
/ / method 1: (function () {… } () (function () {alert ('hello world, I am coming');} ()); / / when the function has parameters, sometimes it is preceded to ensure execution; / / when you see someone else's plug-in, you will find that there is a ";" at the beginning of the page js, so that even if there is an error in the page js, loading and running his plug-in is guaranteed to run, such as:; (function (e) {alert (e);} (' hello world')) / / method 2: (function () {… }) () (function () {alert ('hello');}) (); / / sometimes preceded by an operator, usually! This is the end of the article on "how JavaScript implements self-executing functions" with voidships function () {alert ('hello');} (); void function () {alert (2);} (). I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.