zoukankan      html  css  js  c++  java
  • (function(){...}())与(function(){...})()

      (function(){

            ......

      }())

     或 

     (function(){   

            ......

      })()

     匿名函数自调用,也就是说,定义一个匿名函数,然后马上调用它。 因为这个匿名函数的函数体相当于提供一个匿名的名字空间,这样就不会与用户自定义的JS函数、变量、对象发生冲突了,不失为是一种很好的解决命名空间问题的方法。

     例如json2.js

    (function () {
    
        function f(n) {
            // Format integers to have at least two digits.
            return n < 10 ? '0' + n : n;
        }
    
        if (typeof Date.prototype.toJSON !== 'function') {
    
            Date.prototype.toJSON = function (key) {
    
                return isFinite(this.valueOf()) ?
                       this.getUTCFullYear()   + '-' +
                     f(this.getUTCMonth() + 1) + '-' +
                     f(this.getUTCDate())      + 'T' +
                     f(this.getUTCHours())     + ':' +
                     f(this.getUTCMinutes())   + ':' +
                     f(this.getUTCSeconds())   + 'Z' : null;
            };
    
            String.prototype.toJSON =
            Number.prototype.toJSON =
            Boolean.prototype.toJSON = function (key) {
                return this.valueOf();
            };
        }
    
        ......
    }());


    例如jquery.js

    (function( window, undefined ) {
    
         .....
      
    })(window);
  • 相关阅读:
    iOS button总结
    蓝鸥 UI 考试 绝密
    iOS UI 21 消息通知
    iOS UI 21 单例中的线程
    iOS UI 21 线程
    iOS UI 21 动画
    iOS UI 20 音乐播放器
    深入Objective-C的动态特性
    符合iOS系统兼容性需求的方法
    IOS用NSLog做 debug调试
  • 原文地址:https://www.cnblogs.com/jasenkin/p/javascript_self_call_namespace.html
Copyright © 2011-2022 走看看