2013-03-04 133 views
-5

在下面的代码中,我正在寻找访问getEnvironment函数返回的环境对象。我将如何去我的代码中的其他地方访问此对象?如何从JavaScript函数返回对象并访问返回的对象?

window.EXAMPLE = { 

     config : { 
      local: 'http://localhost:8888/example', 
      staging_v2: 'http://example.com/staging', 
      production: 'http://example.com', 
      image_path: '/images/', 

     }, 

     getEnvironment : function() { 
      if (window.location.href.indexOf(EXAMPLE.config.local) > -1) { 
       var environment = { 
        path : EXAMPLE.config.local + EXAMPLE.config.image_path, 
       } 
       return environment; 
      } 

      if (window.location.href.indexOf(EXAMPLE.config.staging_v2) > -1) { 
       var environment = { 
        path : EXAMPLE.config.staging_v2 + EXAMPLE.config.image_path, 
       } 
       return environment; 
      } 

      if (window.location.href.indexOf(EXAMPLE.config.production) > -1) { 

       var environment = { 
        path : EXAMPLE.config.production + EXAMPLE.config.image_path, 
       } 
       return environment; 
      } 
     }, 

    } 
+0

请花时间清楚地用语言描述你的问题,而不是仅仅倾销的代码块,并期望别人猜测你'试着问。 – 2013-03-04 02:35:47

+1

你能澄清吗?如果您从函数返回对象,则可以正常访问该对象。例如。 'someFunc()propOfObject;'。 – 2013-03-04 02:36:15

回答

3

由方法或函数返回的对象的行为与其他对象没有区别。下面是使用方法在您的示例返回的environment对象的示例:

var env = EXAMPLE.getEnvironment(); 
console.log(env.path);