While looking at various JavaScript libraries, I have noticed a relatively commo
ID: 647873 • Letter: W
Question
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:
DOM4
ContentLoaded
EasyXDM
fingerprintjs
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).
Explanation / Answer
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.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.