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: /
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: /
).
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.
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/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/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.
Share Improve this question asked Sep 25, 2015 at 18:15 orcamanorcaman 6,59110 gold badges57 silver badges71 bronze badges 4- Simply put, you have a syntax error. :) – isherwood Commented Sep 25, 2015 at 18:21
- Also this is a duplicate but I can't find it. – Felix Kling Commented Sep 25, 2015 at 18:22
-
1
var log = function(){ console.log('this function is called'); }(42)
– Travis J Commented Sep 25, 2015 at 18:28 - What have you learned about relying on automatic semicolon insertion? – canon Commented Sep 25, 2015 at 18:33
2 Answers
Reset to default 13The 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`