Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Why: X[i] = x[i] = undefined when x[i] defined? Date: Fri, 17 Oct 2014 21:29:01 +0200 Organization: PointedEars Software (PES) Lines: 67 Message-ID: <2394444.guKU09TTg3@PointedEars.de> References: <95g24apb19udm64v45cehj91b6nj7uo3jb@4ax.com> <0ol24alq2cr6m3hosessv298crkhsnh7ck@4ax.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1413574143 17821 eJwVyMERACEIBLCWDmEXKEcU+i/BuTwDpfC4ETQMpuz2l0unOqrQRWf0xt/3WKzMcnaQoj1bHjJaEaQ= (17 Oct 2014 19:29:03 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Fri, 17 Oct 2014 19:29:03 +0000 (UTC) User-Agent: KNode/4.14.1 X-User-ID: eJwFwQkBwDAIA0BLo5RA5PAU/xJ2ZwpB+4Xh2to6Ql5GZacEvoWdEWfb410KT6mXv4iBH/oMxYjs1WIQ8wNKxxU8 Cancel-Lock: sha1:Qa3wDgTW2HpDTJQoRBlIlu+dwwE= X-NNTP-Posting-Host: eJwFwQkBwDAIA0BLPE0KchgM/xJ6B6ey7yF4sFgZSzHrNEZEqvn6dfuw0ywV4WyLoLym68cD/n8QvQ== Xref: csiph.com comp.lang.javascript:25929 Ed Jay wrote: > On Fri, 17 Oct 2014 09:29:54 -0700, Ed Jay wrote: >>Code - function1: >> >>x_pos_array = []; >>y_pos_array = [] >>function make_div(e) { //mouse event >>var marker_cnt = x_pos_array.length; >>var x_pos = e.x; >>var y_pos = e.y; >>if(some condition is met) { >>var div = document.createElement('div'); >>image_holder.appendChild(div); >>div.id = 'mrkr'+marker_cnt; >>//style the div and add onmouseover listener >>x_pos_array.push(x_pos); >>y_pos_array.push(y_pos); >>}} >> >>Code function 2 >>x_pos_out = []; >>y_pos_out = []; >>function() { >>var marker_cnt = x_pos_array.length; >>for (var i = 0; i < marker_cnt; i++) { >>if (x_pos_array[i] > 0) { >>x_pos_out[i] = x_pos_array[i]; >>y_pos_out[i] = y_pos_array[i]; >>alert(i + ") x_pos_out[i] = " + x_pos_out[i]); >>//undefined for x_pos_array[i] > 0 >> >>} >>} > Solved. > //do something with x_pos_out and y_pos_out > Neglected to declare div as an array. The relevant ECMAScript implementations use dynamic type-checking. Therefore, you cannot declare something as an array; you can only declare a variable and assign to it a reference to an Array instance, after which you can assign any other value to it. What you actually have neglected is to declare “x_pos_array” and “y_pos_array” variables by using the “var” keyword: var x_pos_array = []; var y_pos_array = []; Not doing this causes your code to be more error-prone than it needs to be. You might be accidentally accessing a property of the global object, and your code will maybe compile, but certainly not run in ES5+ strict mode. The code that you have posted is hard to read. If you wish your code to be reviewed here by many, pretty printing, e.g. indentation, is advised. It will also help you to see any errors in your code more easily. See the FAQ for details. Please provide a working contact e-mail address in the From header field, see also . -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not Cc: me. / Bitte keine Kopien per E-Mail.