我需要下面的js代码的解释,[1]在功能上做了什么?
function getURLParameter(url, name) {
return (RegExp(name + '=' + '(.+?)(&|$)')
.exec(url)||[,null])[1];
}
var id = getURLParameter(url, 'id');
console.log(id); //1
// let say the example of url is
// index.php?id=1&user=xxx
什么是[1]
在返回语句中使用的呢?
我迷惑于它是如何获得的值为1的ID
它访问所述阵列的所述第二元件。 – Li357
您可以进行调试并查看自己。 – Batman25663
'.exec(URL)的返回值'可能是一个数组。万一'.exec(URL)'不返回一个数组,我认为'|| [,空])'默认在'[,空]'作为数组索引到,最后的'[1]'索引到当前正在操作的值的任何数组中。所以,你应该要么得到通过.exec(URL)返回的数组或一个空的第二个元素,如果.exec(URL)返回null。但正如@ Batman25663所说,你应该通过调试来验证。 – anandsun