Is the following code valid?
var i; var objs={}; for (i=0; i <10; i++) { objs.i=new FooObject(); } alert(objs.4.someMethod());
If not, how should I rewrite it to accomplish what I want?
You should change your code as follows:
var i; var objs = {}; for (i = 0; i < 10; i++) { objs[i] = new FooObject(); } alert(objs[4].someMethod());
var i; var objs = new Array(); for(i = 0; i < 10; i++) { objs.push(new FooObject()); } objs[4].someMethod();
1. , 2. . , for...in 3. , :
for...in
var x; var myItems = new Array(); myItems[0] = "Foo"; myItems[9] = "Bar"; myItems[5] = "Fiz"; for (x in myItems) { alert(myItems[x]); }
1 http://www.w3schools.com/js/js_variables.asp2 http://www.w3schools.com/js/js_obj_array.asp3 http://www.w3schools.com/js/tryit.asp?filename = tryjs_array_for_in
You cannot use numbers as variable names, because direct numbers exist as their own object set in Javascript (i.e. you can think of 4 as already a global variable that you cannot override).
Source: https://habr.com/ru/post/1713077/More articles:С# LinkButton.PostBackUrl - Новое окно без JS - c#Perforce submit electronic signature collection - authenticationQt signal for a particular object slot - qtIs there a way to make progress on loading using the HttpPostedFile class? - jsonDownload large files - asp.nethttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1713078/does-xml-serialization-require-properties-to-be-readwrite&usg=ALkJrhjONbhgWXFHzZMkDyQHsFULmKcSfwRemoving a recently used query from a list on the Work Items tab of pending changes - tfsCocoa NSTableView multiple choice without command key - cocoaShould I create a clustered index in a fact table? never? is always? - sql-serverhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1713082/how-do-i-kill-a-thread-if-it-takes-too-long-in-c&usg=ALkJrhiODJEi8HpNVl4m0CXSc5AoRImtyQAll Articles