Javascript:如何从全局代码访问本地函数

时间:2016-11-06 20:36:04

标签: javascript html

我在一个立即调用的函数表达式中有一个函数,我希望能够全局访问该函数,而不会以这种方式进行词法作用。

如何通过名为“App”的命名空间访问此“私有”功能?

indew.html:

    <html>
        <head>
            <meta charset="utf-8">
            <title>Test</title>
            <script src="filtering.js"></script>

        </head>
        <body>
            <article>
                <p>
                    Computer graphics is widespread today. Computer imagery is found on televi
                    surfaces, illumination sources, and so forth, perhaps with a dynamic (time) component".[3]
                </p>
            </article>
        <section id="result"></section>

        <script>
            App.showOccurenceFiltering();  // here is my wrong call
        </script>
    </body>
</html>

js script:

var App = (function () {
function showOccurenceFiltering() {
    "use strict";
    var filteredWordsArray = filtering(),
        resultView = "<ol>";
    for (var i = 0; i < filteredWordsArray.length; i++) {
        var partWord = filteredWordsArray[i].substring(0, filteredWordsArray[i].indexOf(" ")),  // de 0 jusqua l espace : la partie mot
            partNumber = filteredWordsArray[i].substring(filteredWordsArray[i].indexOf(" ") + 1); // de l'espace à la fin : la partie number
        resultView += "<li>" + partWord + " (" + partNumber + ")</li>";
    }
    resultView += "</ol>";
    document.getElementById('result').innerHTML = resultView;
}
}(App));

所以我收到了一个未接来电的错误,就像显示了捕获: enter image description here

我该如何解决我的问题?

3 个答案:

答案 0 :(得分:3)

您需要将该功能公开给全局范围。您可以使用命名空间执行此操作,如下所示:

(function () {
   function showOccurenceFiltering() {
    "use strict";
    var filteredWordsArray = filtering(),
        resultView = "<ol>";
    for (var i = 0; i < filteredWordsArray.length; i++) {
        var partWord = filteredWordsArray[i].substring(0, filteredWordsArray[i].indexOf(" ")),  // de 0 jusqua l espace : la partie mot
            partNumber = filteredWordsArray[i].substring(filteredWordsArray[i].indexOf(" ") + 1); // de l'espace à la fin : la partie number
        resultView += "<li>" + partWord + " (" + partNumber + ")</li>";
    }
    resultView += "</ol>";
    document.getElementById('result').innerHTML = resultView;
   }

   // Prepare a "dummy" object
   var obj = {};

   // Attach the private function to this object
   obj.showOccurenceFiltering = showOccurenceFiltering;

   // Attach the dummy object to the global scope in a controlled namespace:
   window.App = obj;

}());

然后你可以像这样访问这个函数:

 App.showOccurenceFiltering();

答案 1 :(得分:0)

您可以执行以下操作:

var App = (function(){
var obj = {};
obj.showOccurenceFiltering = function() {
    "use strict";
    var filteredWordsArray = filtering(),
        resultView = "<ol>";
    for (var i = 0; i < filteredWordsArray.length; i++) {
        var partWord = filteredWordsArray[i].substring(0, filteredWordsArray[i].indexOf(" ")),  // de 0 jusqua l espace : la partie mot
            partNumber = filteredWordsArray[i].substring(filteredWordsArray[i].indexOf(" ") + 1); // de l'espace à la fin : la partie number
        resultView += "<li>" + partWord + " (" + partNumber + ")</li>";
    }
    resultView += "</ol>";
    document.getElementById('result').innerHTML = resultView;
}
return obj;
}());

答案 2 :(得分:0)

您的App变量仍为undefined,因为立即调用的函数表达式未返回值。

相反,将App定义为对象文字,如下所示:

var App = {
    showOccurenceFiltering: function() {
        "use strict";
        var filteredWordsArray = filtering(),
            resultView = "<ol>";
        for (var i = 0; i < filteredWordsArray.length; i++) {
            var partWord = filteredWordsArray[i].substring(0, filteredWordsArray[i].indexOf(" ")),  // de 0 jusqua l espace : la partie mot
                partNumber = filteredWordsArray[i].substring(filteredWordsArray[i].indexOf(" ") + 1); // de l'espace à la fin : la partie number
            resultView += "<li>" + partWord + " (" + partNumber + ")</li>";
        }
        resultView += "</ol>";
        document.getElementById('result').innerHTML = resultView;
    }
};

现在您的通话有效。