另一个函数内的Javascript typeof函数

时间:2018-02-15 06:10:40

标签: javascript jquery function typeof

我只想在已定义函数的情况下运行脚本集,该函数位于包含的JS文件中。 我正在使用jQuery并创建了一个自定义插件,我没有在所有页面中包含它。其中脚本包含对所有页面中的插件的函数调用作为其公共文件。我想检查函数是否已定义,然后才需要执行这些脚本。

对于给定的实例,

JSFiddle example

<!DOCTYPE html>
<html>
<head>
    <title>Testing JS Function Typeof</title>
    <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script type="text/javascript">
        /* This is a plugin code, included as script src */
        (function($) {
            $.fn.includedFunJqReady = function() {
                console.log("You are in includedFunJqReady");
            }
        }(jQuery));
        /* EO plugin code */

        /* JS in page */
        function fun1() {
            console.log('fun');
            function fun2() {
                console.log('fun 2');
            }
        }

        jQuery(document).ready(function(){
            console.log("Type of includedFunJqReady is: " + typeof(includedFunJqReady));
            $('body').includedFunJqReady();
        });
    </script>
</head>
<body>
<script type="text/javascript">
    document.write("Type of fun 1 is: " + typeof(fun1));
    document.write("<br />");
    document.write("Type of fun 2 is: " + typeof(fun2));
    document.write("<br />");
    document.write("Type of includedFunJqReady is: " + typeof(includedFunJqReady));
</script>
</body>
</html>

我也见过this question and answer,但我想知道如何克服这个问题。

0 个答案:

没有答案