简体   繁体   English

如何使用Javascript或jQuery在函数中设置变量

[英]How do I set a variable in a function using Javascript or jQuery

I need to move the data out of HTML code and load it on demand. 我需要将数据移出HTML代码并按需加载。

I need to do something like this: 我需要做这样的事情:

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

I can't figure out how to stuff the data from within the function. 我不知道如何从函数中填充数据。 Is there a way of accomplishing this? 有没有办法做到这一点?

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 is a global anyway. storeData无论如何都是全局的。 When you specify processData( data ) you are doing what's called a pass by value. 当指定processData( data )您正在执行所谓的按值传递。 Basically your making a copy of the data. 基本上,您是在复制数据。 Once the program exits the function, the copy is lost to garbage collection. 程序退出功能后,副本将丢失到垃圾回收中。 An alternative would be to pass by reference, but because it's a global anyway (declared outside the function) there's little point. 另一种选择是通过引用传递,但是因为无论如何它都是全局的(在函数外部声明),所以没有什么意义。

Edit 编辑

read here 在这里阅读

http://snook.ca/archives/javascript/javascript_pass http://snook.ca/archives/javascript/javascript_pass

It might help to know more concrete details since it seems like you might be going about your task in an unusual way. 了解更多具体细节可能会有所帮助,因为您似乎可能以一种不同寻常的方式来完成任务。 There may be a better way of accomplishing what you want. 可能会有更好的方法来完成您想要的。

Have you just tried something as simple as: 您是否尝试过以下简单的操作:

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.

相关问题 Javascript - 如何为函数设置'this'变量 - Javascript - how do i set the 'this' variable for a function 如何在javascript函数中设置新的变量值? - How do I set a new variable value inside a javascript function? 如何将javascript变量设置为内联函数的返回值? - How do I set a javascript variable to the return of an inline function? 如何使用 JavaScript/jQuery 使用 !importanat 设置可变宽度 - How Can I set variable width with !importanat using JavaScript/jQuery 如何使用变量在coffeescript中设置jQuery选择器? - How do I set a jQuery selector in coffeescript using a variable? 如何使用javascript变量使用jQuery隐藏元素 - How do I hide an element with jQuery using a javascript variable 在javascript中使用split函数后如何推送变量? - How do i push a variable after using split function in javascript? 使用Javascript,HTML,JQuery和CSS,如何根据HTML设置变量?选择OnChange,可以立即进行计算和更新吗? - Using Javascript, HTML, JQuery & CSS how can I set a variable depending on a HTML Select OnChange, do calculations and update instantly? 如何在Javascript中设置全局变量? - How do I set a global variable in Javascript? 如何使函数使用在另一个函数JavaScript / jQuery上声明的变量 - How do I make a function use a variable that was declared on another function JavaScript/jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM