• js 立即调用函数


            function makeCounter() {  //不能立即执行
                // 只能在makeCounter内部访问i
                var i = 0;
    
                return function () {
                    console.log(++i);
                };
            }
            var counter = makeCounter(); //对象1
            counter(); // logs: 1 //立刻执行
            counter(); // logs: 2
            var counter2 = makeCounter(); //对象2
            counter2(); // logs: 1
            counter2(); // logs: 2
    
            var foo = function () { console.log("/* code */") }; //直接运行不了
            var foo = function () { console.log("/* code */") }();//直接运行
    
    
            function ff(){ /* code */ }(); // SyntaxError: Unexpected token  出错
            function ff() { console.log("/* code */") } (1); // 式子无异常,无输出
            function foo() { console.log("/* code */")};
                    (1);  //无报错  无输出
    
            (function () { console.log("/* code */") }()); // 推荐使用这个             直接输出
            (function () { console.log("/* code */") })(); // 但是这个也是可以用的     直接输出
    
            var i = function () { console.log("/* code */") }(); //直接输出
            true && function () { console.log("/* code */") }(); //直接输出
            0, function () { console.log("/* code */") }();  //直接输出
    
            !function () { console.log("/* code */") }();//直接输出
            ~function () { console.log("/* code */") }();//直接输出
            -function () { console.log("/* code */") }();//直接输出
            +function () { console.log("/* code */") }();//直接输出
    
            new function () { console.log("/* code */") };//直接输出
            new function () { console.log("/* code */") }();//直接输出
            
            function ff() {
                new function () { console.log("/* code */") };
                !function () { console.log("/* code */") }();
            }
            ff();  直接输出
      !function () { console.log("/* code */1") }(console.log("/* code */2"));//直接输出  先执行2 在执行1
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="jquery-1.10.2.js"></script>
    <script>
       function test() {
           (function($) { console.log("22222")})(jQuery);//输出 22222
           (function($) { console.log($)})(jQuery); //输出
           // jQuery = function( selector, context ) {
           //// The jQuery object is actually just the init constructor 'enhanced'
          // return new jQuery.fn.init( selector, context, rootjQuery );
       //},
           var T=1;
           (function(T) { console.log("11111")})(T); //输出111
           (function($) { console.log("11112")})($); //输出111
       }
    </script>
    </head>
    <body>
    <input type="button" value="测试执行效果" onclick="test()">
    </body>
    </html>

    var xhr1 = function () {
    if (typeof XMLHttpRequest != 'undefined') {
    return new XMLHttpRequest();
    }
    }();

    var xhr2 = (function () {
    if (typeof XMLHttpRequest != 'undefined') {
    return new XMLHttpRequest();
    }
    })();

  • 相关阅读:
    使用数组实现简单线性表功能
    解析.NET 许可证编译器 (Lc.exe) 的原理与源代码剖析
    Entity Framework with NOLOCK
    64位CentOS 6.0下搭建LAMP环境
    如何正确看待Linq的DistinctBy扩展和ForEach扩展
    jQuery最佳实践
    大话数据结构-树
    hdu2534-Score
    WKE——Webkit精简的纯C接口的浏览器
    WM_ERASEBKGND官方解释(翻译),以及Delphi里所有的使用情况(就是绘制窗口控件背景色,并阻止进一步传递消息)
  • 原文地址:https://www.cnblogs.com/enych/p/7930258.html
Copyright © 2020-2023  润新知