I have the following code in my HTML file:
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`