[英]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.