简体   繁体   English

函数有关的Javascript问题-IIFE

[英]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 如何在Touch UI中添加必填下拉字段

My question is what: can somebody tell me what is the end line of the javascript function is doing. 我的问题是:有人可以告诉我javascript函数的结束行是什么。

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

is it some kind of namespacing. 这是某种命名空间吗?

For your reference i am attaching the Javascript code. 供您参考,我附上Javascript代码。

(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. 而那些(“文档,Granite ...等”)是传递给该函数的参数。

The last line is passing arguments to the IIFE (Immediately-invoked function expression). 最后一行将参数传递给IIFE(立即调用的函数表达式)。 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 在函数表达式Granite.author参数传递给函数,将document定义为document ,将Granite.$$并将Granite.authorns

(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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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