在事件监听器中访问对象

时间:2018-09-19 14:53:42

标签: javascript jquery events

如何访问对象foo?我想将其传递给事件监听器。我必须将foo设置为全局吗?

function test(){
  var foo = {
    bar:2
  }
}
$('#bla').on('click',function(){
  foo.bar = 5;
});

1 个答案:

答案 0 :(得分:1)

尝试关注

#JSON file contains name with "ř", after loading data I get <f8> even though I choose encoding of source file
data5 <- fromJSON(file = "Test1801.json")
data6 <- as.data.frame(data5)
data6 <- tolower(data6)   #This and gsub change whole data frame to character values "1"
data6 <- gsub("ř", "r", data6)
var foo={};
test();
function test(){
  foo.bar=2  ;
}
$('#bla').on('click',function(){
  foo.bar = 5;
});