How are functions covered / names placed in a <script> tag?

I am working on a new application that we are developing with Knockout.js and jQuery. I prefer "use strict"in my scripts, but since some of the libraries that we use do not work with "use strict", then I have to use a functional form.

I don’t like to place javascript inside <script>inline tags , so I usually place everything in a separate file so that it can be minimized and scrambled using the preliminary processor.

Given these criteria, I wonder how functions are defined by default when they are created in the script tag. Right now I'm just doing something like this:

$((function(win) {
    "use strict";

    win.myFunction = function () {
        // do stuff
    };
}(window)));

As you can see, I put functions in my object window, so I can name them in my view. I noticed that I can call them in my jQuery templates without their qualifications (i.e.: I can just call myFunctioninstead window.myFunction), but I am worried that this will not work in browsers. Do all browsers allow calling functions placed in a window object without a full name? How can I put a function in a global scope from my anonymous method, as I defined above?

Thank you for understanding!

+5
source share
1 answer

A window object is essentially a global area. Any variable that is not bound to a function is a property of the window object.

, , :

window.myFunction = function() {
    // do stuff
}

, , . , , . jQuery, , - jQuery ( $). , jQuery.each each. , : "" .

+6

All Articles