function f(obj) {
obj = _ => console.log(
'LOCAL object was replaced, how to replace from the outer scope?');
}
f(fetch);
据我了解,这是不可能的,但也许存在一些窍门?
答案 0 :(得分:2)
不,你不能那样做。
相反,最好的选择是返回新对象,并在调用时重新分配:
function f(obj) {
return _ => console.log(
'LOCAL object was replaced, how to replace from the outer scope?');
}
fetch = f(fetch);
或者,您可以传入一个将目标对象作为其状态一部分的容器,并更新该容器的状态:
function f(container) {
container.obj = _ => console.log(
'LOCAL object was replaced, how to replace from the outer scope?');
}
var c = {obj: fetch};
f(c);
// ...use c.obj...
或
function f(container) {
container[0] = _ => console.log(
'LOCAL object was replaced, how to replace from the outer scope?');
}
var c = [fetch];
f(c);
// ...use c[0]...