简体   繁体   中英

Javascript Questions on the function - IIFE

I was asked to ask another question and link it to this thread. How add mandatory dropdown field in Touch UI

My question is what: can somebody tell me what is the end line of the javascript function is doing.

})(document, Granite.$, Granite.author); 

is it some kind of namespacing.

For your reference i am attaching the Javascript code.

(function (document, $, ns) {
    "use strict";

    $(document).on("click", ".cq-dialog-submit", function (e) {
        e.stopPropagation();
        e.preventDefault();

        var $form = $(this).closest("form.foundation-form"),
            title = $form.find("[name='authoringMode']").val(),
            message, clazz = "coral-Button ";

        if(!title){
            ns.ui.helpers.prompt({
            title: Granite.I18n.get("Invalid Input"),
            message: "Please Check Values",
                actions: [{
                    id: "CANCEL",
                    text: "CANCEL",
                    className: "coral-Button"
                }
            ],
            callback: function (actionId) {
                if (actionId === "CANCEL") {
                }
            }
        });
        }else{
                 $form.submit();
        }
    });
})(document, Granite.$, Granite.author);

This is what we otherwise call a self-invoking function:

(function (document, $, ns) { ...
})(document, Granite.$, Granite.author);

and those ("document, Granite...etc) are arguments being passed to that very same function.

The last line is passing arguments to the IIFE (Immediately-invoked function expression). You can learn more here

})(document, Granite.$, Granite.author);

passes parameters to function, defining document as document , Granite.$ as $ and Granite.author as ns within the function expression at

(function (document, $, ns) {

eg,

 var obj = { $:jQuery, author:"abc" }; (function(document, $, ns) { "use strict"; console.log($, ns) }(document, obj.$, obj.author)) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM