Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将参数从一个函数发送到另一个函数_Javascript - Fatal编程技术网

Javascript 如何将参数从一个函数发送到另一个函数

Javascript 如何将参数从一个函数发送到另一个函数,javascript,Javascript,如何从函数func()中获取par值,并使用它附加到另一个函数handlerFunction()中的元素。 PAR是一个ID。在 HooLeLoad()/中,我需要得到 AySpid ID/COD> < /P> 例如: document.getElementById('a_span35') function func(par) { XMLHttp.open("POST", "some.php"); XMLHttp.onreadystatechange = handlerFunction

如何从函数func()中获取par值,并使用它附加到另一个函数
handlerFunction()
中的元素。 PAR是一个ID。在 HooLeLoad()/<代码>中,我需要得到<代码> AySpid ID/COD> < /P> 例如:

document.getElementById('a_span35')

function func(par) {
  XMLHttp.open("POST", "some.php");
  XMLHttp.onreadystatechange = handlerFunction();
  XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  XMLHttp.send("post_name="+par);
}

function handlerFunction() {
  if (XMLHttp.readyState == 4) {
    document.getElementById('a_span').innerHTML=XMLHttp.responseText;
  }
}
您可以这样做:

function func(par) {
XMLHttp.open("POST", "some.php");
XMLHttp.onreadystatechange = function() {
    if (XMLHttp.readyState == 4) {
    document.getElementById('a_span').innerHTML=XMLHttp.responseText;
    }
};
XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
XMLHttp.send("post_name="+par);

}
您可以这样做:

function func(par) {
XMLHttp.open("POST", "some.php");
XMLHttp.onreadystatechange = function() {
    if (XMLHttp.readyState == 4) {
    document.getElementById('a_span').innerHTML=XMLHttp.responseText;
    }
};
XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
XMLHttp.send("post_name="+par);

}

最简单的方法是将
handlerFunction
放在
func
中,这样它就关闭了
par
(但我会在下面给你一个选择):

handlerFunction
是调用
func
上下文的闭包。不要担心术语


还要注意的是,我在上面纠正的代码中有一个错误:您有

XMLHttp.onreadystatechange = handlerFunction();
…它立即调用
handlerFunction
并将其返回值分配给
onreadystatechange
(与
x=foo();
调用
foo
并将返回值分配给
x
)的方式完全相同。应该是:

XMLHttp.onreadystatechange = handlerFunction;

我假设您正在某处创建
XMLHttp
(我建议为
func
的每次调用创建一个新的,而不是重复使用)


备选方案:

您可以通过将对它的调用包装到另一个函数中来保持
handlerFunction
的独立性,可以手动:

function func(par) {
  XMLHttp.open("POST", "some.php");
  XMLHttp.onreadystatechange = function() {
    handlerFunction(XMLHttp, par);
  };
  XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  XMLHttp.send("post_name=" + par);
}

function handlerFunction(xhr, par) {
  if (xhr.readyState == 4) {
    // use par
    document.getElementById('a_span').innerHTML = xhr.responseText;
  }
}
…或使用ES5的
功能#bind

function func(par) {
  XMLHttp.open("POST", "some.php");
  XMLHttp.onreadystatechange = handlerFunction.bind(undefined, XMLHttp, par);
  XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  XMLHttp.send("post_name=" + par);
}

function handlerFunction(xhr, par) {
  if (xhr.readyState == 4) {
    // use par
    document.getElementById('a_span').innerHTML = xhr.responseText;
  }
}

在本机没有的浏览器上,需要为
函数#bind
使用垫片。
bind
所做的是返回一个函数,当调用该函数时,该函数使用一个给定的
值(第一个参数)调用原始函数,并给出相应的参数。

最简单的方法是将
handlerFunction
放在
func
中,这样它就关闭了
par
(但我会在下面给你一个选择):

handlerFunction
是调用
func
上下文的闭包。不要担心术语


还要注意的是,我在上面纠正的代码中有一个错误:您有

XMLHttp.onreadystatechange = handlerFunction();
…它立即调用
handlerFunction
并将其返回值分配给
onreadystatechange
(与
x=foo();
调用
foo
并将返回值分配给
x
)的方式完全相同。应该是:

XMLHttp.onreadystatechange = handlerFunction;

我假设您正在某处创建
XMLHttp
(我建议为
func
的每次调用创建一个新的,而不是重复使用)


备选方案:

您可以通过将对它的调用包装到另一个函数中来保持
handlerFunction
的独立性,可以手动:

function func(par) {
  XMLHttp.open("POST", "some.php");
  XMLHttp.onreadystatechange = function() {
    handlerFunction(XMLHttp, par);
  };
  XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  XMLHttp.send("post_name=" + par);
}

function handlerFunction(xhr, par) {
  if (xhr.readyState == 4) {
    // use par
    document.getElementById('a_span').innerHTML = xhr.responseText;
  }
}
…或使用ES5的
功能#bind

function func(par) {
  XMLHttp.open("POST", "some.php");
  XMLHttp.onreadystatechange = handlerFunction.bind(undefined, XMLHttp, par);
  XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  XMLHttp.send("post_name=" + par);
}

function handlerFunction(xhr, par) {
  if (xhr.readyState == 4) {
    // use par
    document.getElementById('a_span').innerHTML = xhr.responseText;
  }
}

在本机没有的浏览器上,需要为
函数#bind
使用垫片。
bind
所做的是返回一个函数,调用该函数时,使用给定的
这个
值(第一个参数)和给出它的参数调用原始函数。

我选择这个答案。它涵盖了关于闭包的所有需要知道的内容:)我选择这个答案。它涵盖了您需要了解的有关闭包的所有信息:)