[英]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我创建了一个数组
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.