I met some problems today. I didn’t go into them before. I learned some of them through a blog today

  1. First of all, I don’t know why the immediate function can be called
  2. Why write a parenthesis can be called
  3. What’s the advantage? Why don’t we just use the function name instead

According to understand:

  1. Execute the function immediately. When the function is executed only once, it will never be executed again. To avoid the function taking up space, execute the function immediately.
  2. Why is the immediate execution function written this way? Just learn to remember, now let’s see why
(function() {
    console.log("Method one"); }) (); (function() {
    console.log("Method two"); } ())Copy the code

Look at some rules

  1. Only function expressions can be executed symbol execution to this although I have seen the expression, but feel the concept is not clear, I have written the concept of expression article click jump:
  2. The function name is discarded as soon as the executed symbol is executed
  3. The executive symbol is ()
function test() {
    console.log("Performing"); } ()// This is a function declaration* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *function test() {
    console.log("Performing");
}
test() // test is an expression that can be executed by executing symbols* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *var test = function() {
    console.log("Performing"); } ();// Function expressions, which are also expressions, can be executed by executing symbols
 
console.log(test); //undefined* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Copy the code

Then, according to that statement, the expression can be executed using an execution function

  • Since expressions can only be executed immediately by executing symbols, the first example changes below to make it an expression. It’s ready to go
  • Add a plus sign in front of it +, -,! And so can be changed into expression SAO SAO, perhaps is a pen test oh
+ function test() {console.log(" execute "); / / execution} (); console.log(test); //ReferenceError: test is not definedCopy the code

And then look down according to his article

  • If you can do the +, then I can do the () operator, and it works the same way.

  • Since the execution symbol execution name is gone, that simply name I do not write, write also in vain, that simplification does not get the final form we commonly used?

(function() {console.log(" console.log "); }) (); (function() {console.log(" console.log "); } ())Copy the code

The last

Is there two ways to write the immediate execution function? If you want to SAO something add +, -,! , | | can be ah, the main is that later encounter this kind of disgusting topic, don’t be tiger

+ function test() {console.log(" execute "); / / execution} (); console.log(test); //ReferenceError: test is not definedCopy the code

Although AFTER reading this article I am still wondering why the same function is called immediately and then removed from the list

Draw a question mark and search….

  • And then I realized

  • The rule is to execute the function immediately, and then my problem is not solved

I was forced to add a name, he was removed, why??

  • I went to ask a technical bull, he said that the immediate execution of the function is anonymous, after the execution of not delete, put in memory and wait for the New Year? Add a name, it says in the document it’s anonymous, you farted with your pants down??

So now I know exactly what the hell is a function that executes immediately

Note: One last question

Javascript in the seal, in writing Java, I went to see PHP is the need to seal, JS why not, immersed in meditation….

  • Originally it is a language, we learned that it has a seal in the specification, a statement is to end with a seal, but the reason is that js was run in the browser in the early days, and then foreigners do not want to write; Chinese people do not like to write, and the browser belongs to a third party (commercial organization), all programmers do not want to write; So the browser in order to bug less, users use it, and then the browser made a function, if it is a statement to give you the seal number, this is also a mechanism of the browser, he through this mechanism to js engine to achieve this function
  • In Java and PHP they are server-side languages, so they are forced to be sealed
  • But is this title a fight or not? Do we need to fight? Yes. Can we not? Yes

So the question is, the immediate function is an expression so does it need to be marked? Will it judge automatically?

The first one is normal, and the second one can’t be identified, because of the problem of the seal

What about the manual ones?

It is usually preceded by; , preferably before and after;

What’s the point of executing the function immediately

If we need to create a scope in a module file, we think of a function and then execute it, but it’s much easier to write an immediate function. First think of it this way:

function test() {
}
test()
// This is ok but cumbersome, not as simple as executing the function immediately
Copy the code
; (function() {
// scope execution context
// The independent scope of the module
// We can write it like this
var arr = [1.2.3.4.5]
})();
Copy the code

So there’s nothing magical about executing a function immediately if the function tells him to do it

Can also see Daniel zhuanlan.zhihu.com/p/22465092 article, he is speaking and executive function can form an independent scope