I have the following code in my HTML file:
<script type="text/javascript">
window.never = function() {
console.log('this function is never called');
}
(function(d, s, id){
var js, srjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "this.script.does.not.exist.js";
srjs.parentNode.insertBefore(js, srjs);
}(document, 'script', 'streamrail-jssdk'));
</script>
See fiddle: http://jsfiddle.net/sebvaeja/
Looking at the console, you can see that window.never
function is actually called ('this function is never called' is written to the console).
When debugging this with Chrome dev tools, I see in the call stack that the caller was the closure (first line: http://jsfiddle.net/sebvaeja/
).
If I change the never function to be off the global scope:
function never() {
console.log('this function is never called');
}
Then it is not being called.
Can someone please explain why is window.never function being called? What is triggering the call? I guess it's got something to do with the function being on the window object, but I can't see the reasoning behind that.
The window object is supported by all browsers. It represents the browser's window. All global JavaScript objects, functions, and variables automatically become members of the window object. Global variables are properties of the window object. Global functions are methods of the window object.
The JavaScript call() Method The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.
The window object represents an open window in a browser. If a document contain frames (<iframe> tags), the browser creates one window object for the HTML document, and one additional window object for each frame.
Window is the object of browser, it is not the object of javascript. The javascript objects are string, array, date etc.
The Window Object. The window object is supported by all browsers. It represents the browser's window. All global JavaScript objects, functions, and variables automatically become members of the window object. Global variables are properties of the window object. Global functions are methods of the window object.
A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when "something" invokes it (calls it). Example
The this Keyword. In JavaScript, the thing called this, is the object that "owns" the current code. The value of this, when used in a function, is the object that "owns" the function.
The function above does not belong to any object. But in JavaScript there is always a default global object. In HTML the default global object is the HTML page itself, so the function above "belongs" to the HTML page. In a browser the page object is the browser window. The function above automatically becomes a window function.
The function expression is followed by parenthesis:
window.never = function() { ... }
(...)
The line break after the function expression does not terminate the variable statement, so for the parser that's a function call:
function() { ... }(...)
In fact, you are using the very same technique here:
(function(d, s, id){
// ...
}(document, 'script', 'streamrail-jssdk'))
That's a function expression followed by (...)
and it calls the function.
Solution: Add a semicolon after the definition and you are good.
If I change the never function to be off the global scope ... Then it is not being called.
In that case the function definition is interpreted as function declaration, not expression. A function declaration is more like a statement and therefore cannot be part of a CallExpression. The following parenthesis are therefore interpreted as grouping operator (like you intended).
Place the semi-colon after the function declaration:
window.never = function() {
console.log('this function is never called');
};
It's because of the (...)
directly afterwards that triggers the function call.
window.never = function() {
console.log('this function is never called');
}
( ... ) // <-- Triggers call of `window.never`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With