![](/img/trans.png)
[英]Javascript - how do i set the 'this' variable for a function
[英]How do I set a variable in a function using Javascript or jQuery
我需要将数据移出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
我不知道如何从函数中填充数据。 有没有办法做到这一点?
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 )
您正在执行所谓的按值传递。 基本上,您是在复制数据。 程序退出功能后,副本将丢失到垃圾回收中。 另一种选择是通过引用传递,但是因为无论如何它都是全局的(在函数外部声明),所以没有什么意义。
在这里阅读
了解更多具体细节可能会有所帮助,因为您似乎可能以一种不同寻常的方式来完成任务。 可能会有更好的方法来完成您想要的。
您是否尝试过以下简单的操作:
function processData( data )
{
...
return data;
}
storeData = [] ;
storeData = processData( storeData ) ; // first time storeData doesn't contain any data
storeData = processData( storeData ) ; // now storeData contains data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.