Here is some of my javascript:
(function(window) {
window.file = {};
file.i = 0;
for(;;) {
if(file.i++ >= 10) break;
document.body.appendChild(document.createTextNode(file.i))
}
}) ();
Here is some of my javascript:
(function(window) {
window.file = {};
file.i = 0;
for(;;) {
if(file.i++ >= 10) break;
document.body.appendChild(document.createTextNode(file.i))
}
}) ();
Why is window undefined?
Share Improve this question edited Jan 21, 2019 at 19:58 chuck asked Jan 21, 2019 at 19:37 chuckchuck 331 silver badge5 bronze badges 2-
window
is supposed to be the global object. You probably wouldn't need to pass it to function. Also note thatdocument
is a property ofwindow
. – frogatto Commented Jan 21, 2019 at 19:42 - I do that so it is more readable, and sometimes i like to pass a different object rather than window. – chuck Commented Jan 21, 2019 at 19:45
2 Answers
Reset to default 7You need to call the anonymous function with window
as the first argument:
(function(window) {
window.file = {};
file.i = 0;
for(;;) {
if(file.i++ >= 10) break;
document.body.appendChild(document.createTextNode(file.i))
}
}) (window);
Since you provided nothing, window
inside of your function's scope was considered undefined
.
Try
(function(window) {
window.file = {};
file.i = 0;
for(;;) {
if(file.i++ >= 10) break;
document.body.appendChild(document.createTextNode(file.i))
}
})(window);