document.getElementById('button').addEventListener('click', function() {
ApiCall()
console.log(lol)
});
function ApiCall() {
var lol = "this is a test";
}
我已经查看了javascript范围但是如何将此变量传递给第一个函数?
答案 0 :(得分:2)
您应该从函数中返回值。像这样:
document.getElementById('button').addEventListener('click', function() {
var passed_lol = ApiCall()
console.log(passed_lol)
});
function ApiCall() {
var lol = "this is a test"
return lol;
}
<input id="button" type="button" onclick=stop() value="clickme">