Home  >  Q&A  >  body text

javascript - How to understand this js code?

My doubt is what do the parentheses surrounding this function mean, closure?
Then (1) at the end of the method is to bring parameter 1 into this anonymous method?
Does the lexicon of these parentheses mean execution or isolation method?

PHP中文网PHP中文网2648 days ago687

reply all(3)I'll reply

  • typecho

    typecho2017-06-26 10:59:20

    It means to execute immediately, pass in parameter 1

    (function(x){
    
    })(1)

    reply
    0
  • 巴扎黑

    巴扎黑2017-06-26 10:59:20

    This is an immediate execution function. The immediate execution function constructs a function scope, which can play a role in isolation and avoid polluting the global scope. Passing in 1 is the incoming parameter. The immediate execution function can be used to solve closure problems. . But it is not directly related to closure.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-06-26 10:59:20

    Anonymous functions do not have a function name and cannot be called. When the function declaration is enclosed in parentheses, it is no longer a function declaration, but a function expression. Adding () after it means executing the function immediately. 1 is the parameter, which is received by x inside.
    You can understand it as

    var foo = function(x){
        delete x;
        return x;
    };
    foo(1);

    reply
    0
  • Cancelreply