简体   繁体   中英

How to access array/object inside function in javascript ?

Hello I have array outside function like below :

var daily = [];
daily["data"]=[];
daily["data"].push('hello');

function demo()
{
console.log(daily); // not working here
}

How to declare this object as global in Javascript ?

It could be because your function is being hoisted. Try this instead for your function.

var demo = function(){
  console.log(daily);
}

You might also considering just passing that daily variable into your function like so,

var demo = function(d){
  console.log(d);
}

then when you want to call it.

demo(daily);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM