[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

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

Ed Jay

10/17/2014 8:51:00 AM

x_pos_array = [];
x_pos_out = [];

Given that x_pos_array is not empty, e.g., x_pos_array[0] > 0

When
x_pos_out[i] = x_pos_array[i];
x_pos_out[i]) is undefined

But
x_pos_out(no indexing) = x_pos_array[i];
x_pos_out is defined as x_pos_array[i]

Why?
--
Ed Jay (remove 'M' to respond by email)
11 Answers

Denis McMahon

10/17/2014 4:10:00 PM

0

On Fri, 17 Oct 2014 01:50:50 -0700, Ed Jay wrote:

> x_pos_array = [];
> x_pos_out = [];
>
> Given that x_pos_array is not empty, e.g., x_pos_array[0] > 0
>
> When x_pos_out[i] = x_pos_array[i];
> x_pos_out[i]) is undefined
>
> But x_pos_out(no indexing) = x_pos_array[i];
> x_pos_out is defined as x_pos_array[i]
>
> Why?

Sorry, but I'm having trouble following this.

eg:

var i, x1, x2;
x1 = [];
x2 = [];
x1.push( 1 );
for ( i = 0; i < x1.length; i++ ) x2[i] = x1[i];
console.log( "x1[0] = " + x1[0] +"; x2[0] = " + x2[0] );

Output is: "x1[0] = 1; x2[0] = 1"

So I can't really see what the problem is - can you offer code that
demonstrates it?

--
Denis McMahon, denismfmcmahon@gmail.com

Ed Jay

10/17/2014 4:30:00 PM

0

On Fri, 17 Oct 2014 16:10:15 +0000 (UTC), Denis McMahon
<denismfmcmahon@gmail.com> wrote:

>On Fri, 17 Oct 2014 01:50:50 -0700, Ed Jay wrote:
>
>> x_pos_array = [];
>> x_pos_out = [];
>>
>> Given that x_pos_array is not empty, e.g., x_pos_array[0] > 0
>>
>> When x_pos_out[i] = x_pos_array[i];
>> x_pos_out[i]) is undefined
>>
>> But x_pos_out(no indexing) = x_pos_array[i];
>> x_pos_out is defined as x_pos_array[i]
>>
>> Why?
>
>Sorry, but I'm having trouble following this.
>
>eg:
>
>var i, x1, x2;
>x1 = [];
>x2 = [];
>x1.push( 1 );
>for ( i = 0; i < x1.length; i++ ) x2[i] = x1[i];
>console.log( "x1[0] = " + x1[0] +"; x2[0] = " + x2[0] );
>
>Output is: "x1[0] = 1; x2[0] = 1"
>
>So I can't really see what the problem is - can you offer code that
>demonstrates it?

Thanks for the response. I can't see it either. :-)

I create a div for hit zones over a canvas if some conditions are met.
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
//do something with x_pos_out and y_pos_out
}
}
--
Ed Jay (remove 'M' to respond by email)

Ed Jay

10/17/2014 5:53:00 PM

0

On Fri, 17 Oct 2014 09:29:54 -0700, Ed Jay <edMbj@aes-intl.com> wrote:

>On Fri, 17 Oct 2014 16:10:15 +0000 (UTC), Denis McMahon
><denismfmcmahon@gmail.com> wrote:
>
>>On Fri, 17 Oct 2014 01:50:50 -0700, Ed Jay wrote:
>>
>>> x_pos_array = [];
>>> x_pos_out = [];
>>>
>>> Given that x_pos_array is not empty, e.g., x_pos_array[0] > 0
>>>
>>> When x_pos_out[i] = x_pos_array[i];
>>> x_pos_out[i]) is undefined
>>>
>>> But x_pos_out(no indexing) = x_pos_array[i];
>>> x_pos_out is defined as x_pos_array[i]
>>>
>>> Why?
>>
>>Sorry, but I'm having trouble following this.
>>
>>eg:
>>
>>var i, x1, x2;
>>x1 = [];
>>x2 = [];
>>x1.push( 1 );
>>for ( i = 0; i < x1.length; i++ ) x2[i] = x1[i];
>>console.log( "x1[0] = " + x1[0] +"; x2[0] = " + x2[0] );
>>
>>Output is: "x1[0] = 1; x2[0] = 1"
>>
>>So I can't really see what the problem is - can you offer code that
>>demonstrates it?
>
>Thanks for the response. I can't see it either. :-)
>
>I create a div for hit zones over a canvas if some conditions are met.
>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.

Thanks, again, Denis, for your time.
--
Ed Jay (remove 'M' to respond by email)

Thomas 'PointedEars' Lahn

10/17/2014 7:29:00 PM

0

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-ha....

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

Ed Jay

10/17/2014 7:33:00 PM

0

On Fri, 17 Oct 2014 21:29:01 +0200, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>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.
>
Thank you!
>
>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.

OK.
>
>Please provide a working contact e-mail address in the From header field,
>see also <http://www.interhack.net/pubs/munging-ha....

Remove the M and you have it.
--
Ed Jay (remove 'M' to respond by email)

Thomas 'PointedEars' Lahn

10/17/2014 7:52:00 PM

0

Ed Jay wrote:

> [â?¦] Thomas 'PointedEars' Lahn [â?¦] wrote:
>> 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.
>
> Thank you!

You are welcome. As you did not know this, what did you mean by

>>> Neglected to declare div as an array.

? In the code that you posted â??

>>> var div = document.createElement('div');
>>> image_holder.appendChild(div);
>>> div.id = 'mrkr'+marker_cnt;

â?? â??divâ? does not refer to an Array instance; it refers to a â??divâ? element
object.

>> Please provide a working contact e-mail address in the From header field,
>> see also <http://www.interhack.net/pubs/munging-ha....
>
> Remove the M and you have it.

It is not socially acceptable behavior of you to expect others to fiddle
with the â??To:â? field before being able to contact you via e-mail. (Your
signature might not even be displayed.)

Also, please trim your quotations to the relevant minimum (this is also
recommended in the FAQ). Bandwidth may not be as precious as in the early
days of Usenet, but free time still is.

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

Ed Jay

10/17/2014 8:22:00 PM

0

On Fri, 17 Oct 2014 21:51:40 +0200, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>..what did you mean by
>
>>>> Neglected to declare div as an array.
>
>? ?div? does not refer to an Array instance; it refers to a ?div? element
>object.

There are multiple divs, so I should have written in the plural.
>
>>> Please provide a working contact e-mail address in the From header field,
>It is not socially acceptable behavior of you to expect others to fiddle
>with the ?To:? field..

I'm not sure, but in over 20-years, I may have received one or two
emails out of participating on Usenet. OTOH, my email trash folder is
filled with spam, because I didn't cloak my address early on. I do get
your point, however.
>
>Also, please trim your quotations to the relevant minimum

Will do.

Thank you, again.
--
Ed Jay (remove 'M' to respond by email)

John Harris

10/18/2014 9:36:00 AM

0

On Fri, 17 Oct 2014 21:29:01 +0200, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

<snip>
>Please provide a working contact e-mail address in the From header field,
>see also <http://www.interhack.net/pubs/munging-ha....

That document is 15-20 years old and doesn't mention the ReplyTo
header or botnets. At best it should be considered Historic.

I've found that putting a live but easily disposable address in the
ReplyTo header reduces spam. (None to that address in 1 year).

John

Dr J R Stockton

10/18/2014 8:39:00 PM

0

In comp.lang.javascript message <3496960.XX3CMxuZ7L@PointedEars.de>,
Fri, 17 Oct 2014 21:51:40, Thomas 'PointedEars' Lahn
<PointedEars@web.de> posted:

>
>It is not socially acceptable behavior of you to expect others to fiddle
>with the â??To:â? field before being able to contact you via e-mail. (Your
>signature might not even be displayed.)

Before pontificating on what is and what is not socially acceptable, you
should at least endeavour to become socially acceptable yourself, Tanky.

--
(c) John Stockton, nr London UK. ???@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.... - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.

Denis McMahon

10/18/2014 10:18:00 PM

0

On Fri, 17 Oct 2014 09:29:54 -0700, Ed Jay wrote:

>>So I can't really see what the problem is - can you offer code that
>>demonstrates it?
>
> Thanks for the response. I can't see it either. :-)
>
> I create a div for hit zones over a canvas if some conditions are met.
> Code - .....

By inspection your code will not run, it seems to be missing a statement
terminator, have an imbalance of "{" vs "}", and at least one logic test
is not correctly enclosed in "()". Additionally some guesswork is
required to determine where comments terminate and new lines begin.

Hence it is impossible for your code to demonstrate the problem case,
moreover I suspect to do so it would need some minimal html wrapper to go
with it in order to provide a dom context in which it operates.

We can not magically deduce the part(s) of the problem that you fail to
supply us with when explaining what isn't working.

In future, when posting a problem, please post the minimum sufficient
code (including the html document body if needed) that enables the
problem being described to be reproduced in a browser or other javascript
interpreter. This is a generic issue that applies to any request for help
with programming, regardless of language, you should endeavour to post
minimum example code that the reader can run to reproduce the actual
problem you are trying to solve.

The problem you appear to be asking about (see the subject line) is that
you are experiencing a case where assigning values to array s, and then
assigning array t to be equal to array s, t[n] is undefined when s[n] is
defined.

Nothing in the code you posted demonstrates this, at least in part
because the code isn't executable due to the above mentioned errors.

1) Develop code (or possibly in your case an html document with embedded
script element) that demonstrates the exact problem.

2) Cut the document down to the minimum code that shows the problem.

3) Use /* ..... */ style comments so it is clear where comments end,
especially when posting code to usenet where line wrapping may not be
preserved.[1]

4) Post the whole document here if fairly short (eg less than 30 lines or
so) or if larger post a link to it on a web server.

5) Remember step 2, it really is important. The more code that is
irrelevant to the problem that we need to trawl through and try and
understand the more likely we are to lose interest. We are not going to
do step 2 for you, you need to do that as part of the we fix your code
deal.

[1] I've been using usenet and similar for 25+ years, and I still haven't
worked out a consistent and guaranteed way to preserve line breaks in
code postings[2] that allows for the vagaries of all the different
clients and the way they process received text and quote it. Don't even
bother trying!

[2] Apart from posting the source somewhere and posting a link to it in
the message, which upsets some purists, but which is sometimes the only
way to do it.

--
Denis McMahon, denismfmcmahon@gmail.com