js將方法作為參數調用


<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>js調用</title>    
    <script src="cssjs/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
        $().ready(function () {
            $.dialog = function (settings) {
                if ($.isFunction(settings.okCallback)) {
                    if (settings.height == null) {
                        if (settings.okCallback.apply() != false) {
                            alert("1");
                        }
                    } else {
                        
                        if (settings.okCallback.call(this, settings.height) != false) {
                            alert("2");
                        }
                        
                        /*
                        if (settings.okCallback.apply(this, arguments) != false) {
                            alert("2");
                        }
                        */
                    }
                }
            }
        });        
    </script>
    <script type="text/javascript">
        $(function () {
            $.dialog({
                okCallback: print,
                height: {data:"你好"}
            });
        });
    function print(ee1) {
        alert("print(ee1)");
        
        alert(ee1.data);
        
        /*
        alert(ee1.height.data);
        */
    }
    /*
    function print(a, b, c, d) {
    alert(a + b + c + d);
    }
    function example(a, b, c, d) {
    //用call方式借用print,參數顯式打散傳遞
    print.call(this, a, b, c, d);
    //用apply方式借用print, 參數作為一個數組傳遞,
    //這里直接用JavaScript方法內本身有的arguments數組
    print.apply(this, arguments);
    //或者封裝成數組
    print.apply(this, [a, b, c, d]);
    }
    //下面將顯示"背光腳本"
    example("背", "光", "腳", "本"); 
    */
    </script>
</head>
<body>
    
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM