[英]Javascript variable declaration syntax
I'm taking charge of a javascript webapp. 我负责一个JavaScript网络应用。 It's very complex, and I'm having some trouble with syntax:
这非常复杂,语法上有些麻烦:
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;
};
in particular parseQueryString().CustomConfigPath
and the var result = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m;
特别是
parseQueryString().CustomConfigPath
和var result = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m;
The first seems to be a sort of property access by the parseQueryString function. 第一种似乎是parseQueryString函数对属性的一种访问。
The second seems an array declaration, but without the Array() constructor. 第二个似乎是数组声明,但没有Array()构造函数。 Also, the
m
value is recalled without the presumed array result in the while cycle. 另外,在while循环中调用
m
值时不会获得假定的数组结果。
By looking at: 通过查看:
parseQueryString().CustomConfigPath
you can say that parseQueryString()
is expected to return an object with CustomConfigPath
property. 您可以说
parseQueryString()
应该返回具有CustomConfigPath
属性的对象。
And from this: 从这:
var result = {};
you see that result
is indeed an object ( {}
is an empty object literal). 您会看到
result
确实是一个对象( {}
是一个空的对象文字)。 It is not an array. 它不是数组。 Later, in a loop, there is:
稍后,有一个循环:
result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
so we're assigning properties to the result
object. 所以我们要为
result
对象分配属性。 One of this properties will be (as we can expect) a CustomConfigPath
. (我们可以预期)此属性之一是
CustomConfigPath
。 This will be taken from the query string - we'll use regular expression to do this: re = /([^&=]+)=([^&]*)/g
. 这将从查询字符串中获取-我们将使用正则表达式执行此操作:
re = /([^&=]+)=([^&]*)/g
。 So address of the webpage on which this code is executed looks like: http://example.com/something?SomeKey=value&CustomConfigPath=something
. 因此,执行此代码的网页地址如下:
http://example.com/something?SomeKey=value&CustomConfigPath=something
: http://example.com/something?SomeKey=value&CustomConfigPath=something
。
General syntax for assigning properties to an object is: 将属性分配给对象的一般语法为:
result[key] = value;
// key -> decodeURIComponent(m[1])
// value -> decodeURIComponent(m[2])
parseQueryString().CustomConfigPath
calls the parseQueryString
function, which returns an object. parseQueryString().CustomConfigPath
调用parseQueryString
函数,该函数返回一个对象。 Then it accesses the CustomConfigPath
property of that object. 然后,它访问该对象的
CustomConfigPath
属性。 A common idiom for the first 4 lines of the function is: 该函数的前4行的常见用法是:
var customConfigPath = parseQueryString().CustomConfigPath || "/.customer-configuration";
var result = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m
is a declaration of 4 different variables, not an array: var result = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m
是4个不同变量的声明,不是数组:
result
is an empty object result
是一个空对象 queryString
is the query string from the current URL, with the ?
queryString
是当前URL中的查询字符串,带有?
removed. re
is a regular expression re
是一个正则表达式 m
is a variable that isn't initialized, it will be assigned later in the while
loop. m
是未初始化的变量,它将在while
循环中稍后分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.