如何通过回调测试访问potatos,不能修改potatos匿名功能?绑定等等不起作用,有人知道吗?
//This cannot be changed
!function(a){
//
let potatos = {
count: 999
}
//
let garden = {
/**
*
*/
callbacks: [],
/**
*
*/
showInfo: function() {
this.callbacks.forEach((cb) => {
cb();
})
}
}
//
global.garden = garden;//or window.garden
}();
/**
*
*/
function test() {
console.log(potatos);//Error
}
garden.callbacks.push(test);
garden.showInfo();
没有JavaScript使用lexical scope作为变量。
test
被声明为存在potatoes
的任何范围之外。它无法访问该变量。