Reputation: 2301
Why do some programmers pass in the global window
object, which I understand is for better performance and minifying benefits, but also other dependency objects / functions from the exact same global space window
but passed in through another parameter?
But then they also use functions from window
that they don't pass in explicitly, like setTimeout()
.
(function (dependency1, dependency2, dependency3, dependency4) {
'use strict';
//body
window.setTimeout(function() { // <--
//body
}, 1000);
//etc.
}(window, window.document, window.AjaxRequest, window.util.padNumber)); // <--
Are there any benefits to using the top variant over the following, which basically still has access to the same dependencies?
(function (window) {
'use strict';
//body
window.setTimeout(function() {
//body
}, 1000);
//etc.
}(window));
Upvotes: 1
Views: 1031
Reputation: 592
Upvotes: 2
Reputation: 3023
This is done in order to stick to OPEN/CLOSE paradigm.
the open/closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"
To close the modification, you pass the dependencies as parameter. This allows a programmer to change it on the fly without worrying about no of places it is being used in the function.
Upvotes: 1