Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the function called? JavaScript / Window

Tags:

javascript

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.

like image 991
orcaman Avatar asked Sep 25 '15 18:09

orcaman


People also ask

What does window mean JavaScript?

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.

Why do we call a function in JavaScript?

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.

Why is this the window object in JavaScript?

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.

Is window a JavaScript object?

Window is the object of browser, it is not the object of javascript. The javascript objects are string, array, date etc.

What is a window object in JavaScript?

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.

What is a JavaScript function?

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

What is the this keyword in JavaScript?

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.

Which object does the function above belong to in JavaScript?

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.


2 Answers

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).

like image 80
Felix Kling Avatar answered Sep 28 '22 18:09

Felix Kling


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`
like image 32
Spencer Wieczorek Avatar answered Sep 28 '22 17:09

Spencer Wieczorek