Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #25929

Re: Why: X[i] = x[i] = undefined when x[i] defined?

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Why: X[i] = x[i] = undefined when x[i] defined?
Date 2014-10-17 21:29 +0200
Organization PointedEars Software (PES)
Message-ID <2394444.guKU09TTg3@PointedEars.de> (permalink)
References <fel14a5kmcg35jimb7hq4afq4tcnvfhohv@4ax.com> <m1rf17$2te$2@dont-email.me> <95g24apb19udm64v45cehj91b6nj7uo3jb@4ax.com> <0ol24alq2cr6m3hosessv298crkhsnh7ck@4ax.com>

Show all headers | View raw


Ed Jay wrote:

> On Fri, 17 Oct 2014 09:29:54 -0700, Ed Jay <edMbj@aes-intl.com> 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 <http://www.interhack.net/pubs/munging-harmful/>.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Why: X[i] = x[i] = undefined when x[i] defined? Ed Jay <edMbj@aes-intl.com> - 2014-10-17 01:50 -0700
  Re: Why: X[i] = x[i] = undefined when x[i] defined? Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-17 16:10 +0000
    Re: Why: X[i] = x[i] = undefined when x[i] defined? Ed Jay <edMbj@aes-intl.com> - 2014-10-17 09:29 -0700
      Re: Why: X[i] = x[i] = undefined when x[i] defined? Ed Jay <edMbj@aes-intl.com> - 2014-10-17 10:52 -0700
        Re: Why: X[i] = x[i] = undefined when x[i] defined? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-17 21:29 +0200
          Re: Why: X[i] = x[i] = undefined when x[i] defined? Ed Jay <edMbj@aes-intl.com> - 2014-10-17 12:32 -0700
            Re: Why: X[i] = x[i] = undefined when x[i] defined? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-17 21:51 +0200
              Re: Why: X[i] = x[i] = undefined when x[i] defined? Ed Jay <edMbj@aes-intl.com> - 2014-10-17 13:21 -0700
              Re: Why: X[i] = x[i] = undefined when x[i] defined? Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-10-18 21:38 +0100
          Re: Why: X[i] = x[i] = undefined when x[i] defined? John Harris <niam@jghnorth.org.uk.invalid> - 2014-10-18 10:36 +0100
      Re: Why: X[i] = x[i] = undefined when x[i] defined? Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-18 22:17 +0000
        Re: Why: X[i] = x[i] = undefined when x[i] defined? Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-10-19 22:54 +0100

csiph-web