0

From HTML5 Mobile Boilerplate's helper.js:

(function(document){
    //all stuff here
})(document);

What does this snippet do or when does it run?

1
  • This question is similar to: What is the (function() { } )() construct in JavaScript?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Nov 12, 2024 at 12:42

4 Answers 4

3

This is a closure, it defines a method which takes an argument document and immediately calls it with document as the parameter.

It runs as soon as it's finished evaluating - so basically straight away.

Sign up to request clarification or add additional context in comments.

Comments

1

It creates a temporary, anonymous function and calls it with an argument called document. Presumably it has some local variables that it is hiding from the enclosing scope.

Comments

1

This is a javascript function that executes immediately when the browser encounters it while parsing the page. The function takes one parameter, which is the window.document property (as passed in at the bottom of the function.

Comments

1

If you say:

(function(var1){/*stuff*/})(var2)

That immediately calls the function and passes var2 to the function. Note that the function is anonymous and cannot be called directly. You can read about anonymous functions in general and anonymous functions in Javascript here:

http://en.wikipedia.org/wiki/Anonymous_function#JavaScript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.