所以我在网上找到了这个脚本,除了两部分我都明白了:
1) var first = getUrlVars()["id"]; var second = getUrlVars()["page"];
我从来没有见过后面有括号的函数,它是什么意思/做什么?
2) 函数(m,键,值)
这些参数来自哪里(m、key、value)?
window.location 将是:http://papermashup.com/index.php?id=123&page=home
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
var first = getUrlVars()["id"];
var second = getUrlVars()["page"];
alert(first);
alert(second);
最佳答案
var first = getUrlVars()["id"];
这一行是调用getUrlVars()
函数,然后访问返回对象的id
属性,然后设置first
变量到该属性的值。
function(m,key,value)
是一个匿名函数,作为参数传递给 String
对象上的 replace
函数。 Docs here .本质上,它是将一个函数传递给 replace
函数的这个参数:
A function to be invoked to create the new substring (to put in place of the substring received from parameter #1). The arguments supplied to this function are described in the "Specifying a function as a parameter" section below.
关于javascript - 调用 Javascript 函数后的括号是做什么用的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37843195/