我正在调用此函数,将结果分配给回调中的变量,然后记录结果,但我一直未定义。
var id;
test.getID(function(result) {
id=result;
});
console.log(id);
如果我将其更改为下面的代码,那么我可以看到记录的 id。
var id;
test.getID(function(result) {
id=result;
console.log(id);
});
您知道我可以做什么才能访问 getID 函数的结果吗?
The getID
函数需要调用它的参数才能看到id
change.
由于您没有提供其实现,因此我们假设它是这样的。狠抓落实getID
,它以一个函数作为参数,f
,然后调用它。这是当id
将被设置。
var id;
var test = {
getID: function(f){
var result = 666; //assume result comes from somewhere
f(result); //Note: this is where your function is getting invoked.
}
};
test.getID(function(result) {
id = result;
});
console.log(id); //prints 666
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)