我需要将数据移出HTML代码并按需加载。
我需要做这样的事情:
function processData( data )
{
if ( data.length===0 )
{
data = get data from server using Ajax or even...
data = [['2011-06-01',1],['2011-06-02',3]] ; // just for educational purposes
}
else go do stuff with data ;
}
storeData = [] ;
processData( storeData ) ; // first time storeData doesn't contain any data
processData( storeData ) ; // now storeData contains data
我无法弄清楚如何从函数中填充数据。有没有办法实现这个目标?
答案 0 :(得分:1)
function processData()
{
if ( storeData.length===0 )
{
storeData = get data from server using Ajax
}
else go do stuff with storeData ;
}
storeData = [] ;
processData( storeData ) ; // first time storeData doesn't contain any data
processData( storeData ) ; // now storeData contains data
无论如何, storeData是一个全球性的。当您指定processData( data )
时,您正在执行所谓的值传递。基本上你是制作数据的副本。程序退出函数后,副本将丢失到垃圾回收中。另一种方法是通过引用传递,但因为它无论如何都是全局的(在函数之外声明),没有什么意义。
在这里阅读
答案 1 :(得分:0)
了解更具体的细节可能会有所帮助,因为您似乎可能以不寻常的方式处理您的任务。可能有更好的方法来完成你想要的东西。
你刚刚尝试了一些简单的事情:
function processData( data )
{
...
return data;
}
storeData = [] ;
storeData = processData( storeData ) ; // first time storeData doesn't contain any data
storeData = processData( storeData ) ; // now storeData contains data