2

While looking at various JavaScript libraries, I have noticed a relatively common practice of passing either the window or the document object as part of the arguments given to the library initialization function.

Examples:

While I understand very well why that would be done for libraries that are designed to run on non-browser environments, I do not know why that is done for libraries that seem designed only for client browser environments. (Such as all the examples given above).

2
  • just because they are meant to be only used in browsers doesn't mean that they will (users are stupid) Commented Nov 19, 2014 at 11:17
  • @DanD. That should be an answer ;)
    – back2dos
    Commented Nov 19, 2014 at 11:31

1 Answer 1

3

It also helps to speed up the library as window or document is now locally bound. And this also ensures that if latter the user, replaces the window object with say: window = "Help, I'm trapped in a window overwriting assignment statement.";; the library continues to work.

1
  • 3
    Also, it helps JS code minifiers in their work, as they can change the parameter name inside the function.
    – jhominal
    Commented Nov 20, 2014 at 10:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.