简体   繁体   English

以后可以访问添加到 javascript 数组中的数据吗?

[英]Can data added in javascript arrays be accessed later?

I am new to Javascript, is there a way to store data in an array and be able to access it later.我是 Javascript 的新手,有没有办法将数据存储在数组中,以后可以访问它。 Example :例子 :

I have created an array我创建了一个数组

  • If I click on my HTML file button (add a question), a function generates a random Id for a question block (like id="13819").如果我点击我的 HTML 文件按钮(添加一个问题),一个函数会为一个问题块生成一个随机 ID(如 id="13819")。
  • It is then added to an array of question Id's然后将其添加到问题 Id 的数组中
  • Once the function is done running, does the Id stay in the array once added?函数运行完成后,Id 是否在添加后保留在数组中? Or does the array reset when the function called again?或者当函数再次调用时数组是否重置?
  • If it does reset, how do you make it to not reset?如果它确实重置了,你如何使它不重置?

It depends on where your variable is created.这取决于您的变量的创建位置。 If it is a top level variable(global) you will be able to add new data during the whole users session.如果它是顶级变量(全局),您将能够在整个用户会话期间添加新数据。 On the other side if that variable is created inside a function it will be erased every time you call that function.另一方面,如果该变量是在函数内部创建的,则每次调用该函数时它都会被删除。

Short answer: Yes简短回答:是的

Long answer: It depends长答案:这取决于

It depends on where you created the array.这取决于您创建阵列的位置。 This is known as the scope of the array.这称为数组的范围。

If it is inside the function, the scope of the array is inside the function, so only things inside the function can access the array.如果在函数内部,数组的作用域就在函数内部,所以只有函数内部的东西才能访问数组。 Once the function is finished, the array no longer exists.一旦函数完成,数组就不再存在。

You can create it in the global scope, ie as an attribute of the global window object, but you should try as much as possible to limit the scope of your variables, so that you don't pollute the global namespace可以在全局范围内创建,即作为全局window对象的一个​​属性,但是你应该尽量限制你的变量的范围,这样就不会污染全局命名空间

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java稍后将在哪里存储数据? - Where to store data which will be later accessed by Javascript? 保存HTML元素的数据,以便以后可以访问 - save data for an HTML element such that it can be accessed later 可以像数组一样访问Javascript对象吗? - Can Javascript objects be accessed like arrays? 如何在ASP脚本中设置.data()变量,以便以后可以访问? - How to set .data() variable inside an ASP script so it can be accessed later? 如何创建JavaScript数据类型(通过键和索引访问) - How can I create a javascript data type (accessed with key and index) 稍后在带有 src 的脚本标记中访问的 Javascript 全局范围变量 - Javascript global scope variables accessed later in a script tag with src 为什么已经生成的对象使用“ new”操作可以访问原型方法,该方法稍后会在javascript中添加 - why a object that already generated use 'new' operate can access a prototype's method which added later in javascript Javascript:如果稍后重定向到另一个页面,则无法将数据添加到数据库 - Javascript: Can't add data to database if redirecting to another page later 有没有办法制作一个 function 可以稍后在谷歌浏览器扩展中访问? - Is there a way to make a function that can be accessed later in a google chrome extension? Javascript 在稍后通过 Javascript 添加的元素上执行失败 - Javascript execution failed on elements which were added later through Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM