在一个匿名函数尾部有一段return函数:
return{
      a:function(){
              ......
                 },
      b:function(){
                 ......
                 },
},我想问下各位大大,这个return在这个匿名函数底部有啥用,而且底a,b函数是怎么调用的,还是自动执行????
能详细点说说嘛?感激不尽!!!!!

解决方案 »

  1.   

        <script>
            function getJson(){
                return {
                    a:function(){
                        return "this is a";
                    },
                    b:function(){
                        return "this is b";
                    }
                }
            }
            alert(getJson().a());
            alert(getJson().b());
        </script>
      

  2.   


    我写的这个我能理解...但是我看那个piwik.js中traceker里面的根本就没有调用哎我把源代码发下吧
      

  3.   

    return {
    /*<DEBUG>*/
    /*
     * Test hook accessors
     */
    hook: registeredHooks,
    getHook: function (hookName) {
    return registeredHooks[hookName];
    },
    /*</DEBUG>*/ /*
     * Specify the Piwik server URL
     */
    setTrackerUrl: function (trackerUrl) {
    if (isDefined(trackerUrl)) {
    configTrackerUrl = trackerUrl;
    }
    }, /*
     * Specify the site ID
     */
    setSiteId: function (siteId) {
    if (isDefined(siteId)) {
    configTrackerSiteId = siteId;
    }
    }, /*
     * Pass custom data to the server
     */
    setCustomData: function (customData) {
    if (isDefined(customData)) {
    configCustomData = customData;
    }
    }, /*
     * Set delay for link tracking (in milliseconds)
     */
    setLinkTrackingTimer: function (delay) {
    if (isDefined(delay)) {
    configTrackerPause = delay;
    }
    }, /*
     * Set list of file extensions to be recognized as downloads
     */
    setDownloadExtensions: function (extensions) {
    if (isDefined(extensions)) {
    configDownloadExtensions = extensions;
    }
    }, /*
     * Specify additional file extensions to be recognized as downloads
     */
    addDownloadExtensions: function (extensions) {
    if (isDefined(extensions)) {
    configDownloadExtensions += '|' + extensions;
    }
    }, /*
     * Set array of domains to be treated as local
     */
    setDomains: function (hostsAlias) {
    if (typeof hostsAlias == 'object' && hostsAlias instanceof Array) {
    configHostsAlias = hostsAlias;
    configHostsAlias.push(locationHostnameAlias);
    } else if (typeof hostsAlias == 'string') {
    configHostsAlias = [hostsAlias, locationHostnameAlias];
    }
    }, /*
     * Set array of classes to be ignored if present in link
     */
    setIgnoreClasses: function (ignoreClasses) {
    if (typeof ignoreClasses == 'object' && ignoreClasses instanceof Array) {
    configIgnoreClasses = ignoreClasses;
    } else if (typeof ignoreClasses == 'string') {
    configIgnoreClasses = [ignoreClasses];
    }
    }, /*
     * Set request method (GET or POST; default is GET)
     */
    setRequestMethod: function (method) {
    configRequestMethod = method || 'GET';
    }, /*
     * Override referrer
     */
    setReferrerUrl: function (url) {
    if (isDefined(url)) {
    configReferrerUrl = url;
    }
    }, /*
     * Override url
     */
    setCustomUrl: function (url) {
    if (isDefined(url)) {
    configCustomUrl = url;
    }
    }, /*
     * Override document.title
     */
    setDocumentTitle: function (title) {
    if (isDefined(title)) {
    configTitle = title;
    }
    }, /*
     * Set array of classes to be treated as downloads
     */
    setDownloadClasses: function (downloadClasses) {
    if (typeof downloadClasses == 'object' && downloadClasses instanceof Array) {
    configDownloadClasses = downloadClasses;
    } else if (typeof downloadClasses == 'string') {
    configDownloadClasses = [downloadClasses];
    }
    }, /*
     * Set download class name (i.e., override default: piwik_download)
     * (deprecated)
     */
    setDownloadClass: function (className) {
    if (typeof className == 'string') {
    configDownloadClasses = [className];
    }
    }, /*
     * Set array of classes to be treated as outlinks
     */
    setLinkClasses: function (linkClasses) {
    if (typeof linkClasses == 'object' && linkClasses instanceof Array) {
    configLinkClasses = linkClasses;
    } else if (typeof linkClasses == 'string') {
    configLinkClasses = [linkClasses];
    }
    }, /*
     * Set outlink class name (i.e., override default: piwik_link)
     * (deprecated)
     */
    setLinkClass: function (className) {
    if (typeof className == 'string') {
    configLinkClasses = [className];
    }
    }, /*
     * Add click listener to a specific link element.
     * When clicked, Piwik will log the click automatically.
     */
    addListener: function (element) {
    if (isDefined(element)) {
    addClickListener(element);
    }
    }, /*
     * Install link tracker
     */
    enableLinkTracking: function () {
    if (hasLoaded) {
    // the load event has already fired, add the click listeners now
    addClickListeners();
    } else {
    // defer until page has loaded
    registeredOnLoadHandlers[registeredOnLoadHandlers.length] = function () {
    addClickListeners();
    };
    }
    }, /*
     * Trigger a goal
     */
    trackGoal: function (idGoal, customRevenue, customData) {
    logGoal(idGoal, customRevenue, customData);
    }, /*
     * Manually log a click from your own code
     */
    trackLink: function (sourceUrl, linkType, customData) {
    logLink(sourceUrl, linkType, customData);
    }, /*
     * Log visit to this page
     */
    trackPageView: function (customTitle, customData) {
    logPageView(customTitle, customData);
    }
    };
    }
      

  4.   

    我没有看到哪里调用了trackPageView这个函数,但是我用alert测试的时候,js是走到这里的。难道这个是json用法????
      

  5.   

    就相当于把函数用json的格式存放起来了
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
      <SCRIPT LANGUAGE="JavaScript">
      <!--
      function getHook(){
    alert("hook1")
      }
       function getHook2(){
    alert("hook2")
      }
      var Methods = {};  $(function(){
    Methods.getHook = getHook;
    Methods.getHook2 = getHook2;

    Methods.getHook()
    Methods.getHook2()
      })
      //-->
      </SCRIPT>
     </HEAD>
     <BODY>
     </BODY>
    </HTML>
      

  6.   

    json格式的函数调用,函数就不是全局了,可以节省内存
      

  7.   

    能举个小例子吗?譬如如何执行到a这个函数的
    return{
       a:function(){};
    }
      

  8.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
      <SCRIPT LANGUAGE="JavaScript">
      <!--   var Methods = (function(){  
    return {
    a:function(){alert("a")}
    ,b:function(){alert("b")}
    }
      })();  $(function(){
    // 调用
    Methods.a();
    Methods.b();
      })
      //-->
      </SCRIPT>
     </HEAD>
     <BODY>
     </BODY>
    </HTML>
      

  9.   

    return 了个json对象。
    像楼上那样可以调用