有没有办法从回调中访问这个变量?
Is there any possible way to access this variable from callback?
回调测试如何访问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
存在的任何范围之外声明。它无法访问该变量。
回调测试如何访问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
存在的任何范围之外声明。它无法访问该变量。