2013-10-13 158 views
0

我负责一个JavaScript web应用程序。这是非常复杂的,而且我遇到一些麻烦语法:Javascript变量声明语法

getThemeBaseUrl = function() { 
    var customConfigPath = "./customer-configuration";      
    if (parseQueryString().CustomConfigPath) {       
    customConfigPath = parseQueryString().CustomConfigPath; 
    } 
    var clientId = parseQueryString().ClientId; 

    return customConfigPath + "/themes/" + clientId; 
}; 

parseQueryString = function() { 
    var result = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m; 
    while (m = re.exec(queryString)) { 
    result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]); 
    } 
    return result; 
}; 
特别 parseQueryString().CustomConfigPath

var result = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m;

第一似乎是一种由parseQueryString功能属性访问。

第二个看起来是数组声明,但没有Array()构造函数。此外,m值在while循环中没有推测的数组结果时被调用。

回答

0

通过观察:

parseQueryString().CustomConfigPath 

可以说parseQueryString()有望与CustomConfigPath属性返回一个对象。

而从这个:

var result = {}; 

你看到result确实是一个对象({}是一个空对象文本)。 这不是一个数组。后来,在一个循环中,有:

result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]); 

,所以我们要指定属性的result对象。其中一个属性将会(正如我们所预料的)一个CustomConfigPath。这将取自查询字符串 - 我们将使用正则表达式来执行此操作:re = /([^&=]+)=([^&]*)/g。因此,执行此代码的网页的地址如下所示:http://example.com/something?SomeKey=value&CustomConfigPath=something

为一个对象指定属性一般语法是:

result[key] = value; 
// key -> decodeURIComponent(m[1]) 
// value -> decodeURIComponent(m[2]) 
0

parseQueryString().CustomConfigPath调用parseQueryString函数返回一个对象。然后它访问该对象的CustomConfigPath属性。对于前4行的函数的一个常见的成语是:

var customConfigPath = parseQueryString().CustomConfigPath || "/.customer-configuration"; 

var result = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m是4个不同的变量的声明,而不是一个阵列:

  • result是一个空对象
  • queryString是来自当前URL的查询字符串,其中?已被删除。
  • re是正则表达式
  • m是未初始化的变量,它将在后面在while循环分配。