Prev: FireFoc support forums (Re: FF3.6 Stack output from Too MuchRecursion)
Next: PASSING VALUES BETWEEN SCRIPTS
From: SAM on 10 Mar 2010 11:19 Le 3/10/10 3:13 PM, Jorge a �crit : > On Mar 10, 3:00 pm, Antony Scriven <adscri...(a)gmail.com> wrote: >> >> What exactly do you mean by 'exists'? If I do >> >> var myarray = new Array(5); >> >> does a[3] exist? > > (I guess you meant myarray[3], right ?) > > No, (3 in myarray) would return false. Call it a hole -if you want- > but it's not the same as if you did: > > myarray[3]= undefined; > 3 in myarray; //now it exists > --> true > delete myarray[3]; //creates a "hole" @ index #3 > 3 in myarray; > --> false With which browser ? My Firefox tells me 'undefined' for all that alerts : var b = [1,2,,4,5]; alert(b[2]); var c = [1,2,3,4,5]; delete(c[2]) alert(c[2]); var a = new Array(5); alert(a[2]); As for those ones : var b = [1,2,,4,5]; alert(typeof b[2]); var b = [1,2,3,4,5]; delete(b[2]) alert(typeof b[2]); var a = new Array(5); alert(typeof a[2]); If an element of an array is not present it is not 'null' but only not known ('undefined'). var g = [0, 1, , 3, 4, 5]; g[4] = null; alert('arary length = '+g.length); for(var i in g) alert('g['+i+'] value = ' + g[i] + '\nnull or undefined ? = ' + (g[i]==undefined||g[i]==null)) Probably better with : alert('g['+i+'] value = ' + g[i] + '\nnull or undefined ? = ' + (typeof g[i]=='undefined' || g[i]==null) ) -- sm
From: Antony Scriven on 10 Mar 2010 11:27 On Mar 10, 4:19pm, SAM wrote: > Le 3/10/10 3:13 PM, Jorge a crit : > > > [...] > > > > myarray[3]= undefined; > > 3 in myarray; //now it exists > > --> true > > delete myarray[3]; //creates a "hole" @ index #3 > > 3 in myarray; > > --> false > > With which browser ? > > My Firefox tells me 'undefined' for all that alerts : > > [...] Right: those are the values. 'in' just checks if the property has been created. --Antony
From: Lasse Reichstein Nielsen on 10 Mar 2010 11:35 SAM <stephanemoriaux.NoAdmin(a)wanadoo.fr.invalid> writes: > Le 3/10/10 3:13 PM, Jorge a �crit : >> (I guess you meant myarray[3], right ?) >> No, (3 in myarray) would return false. Call it a hole -if you want- >> but it's not the same as if you did: >> myarray[3]= undefined; >> 3 in myarray; //now it exists >> --> true >> delete myarray[3]; //creates a "hole" @ index #3 >> 3 in myarray; >> --> false > > With which browser ? Hopefully all of them. It's the specified behavior. > My Firefox tells me 'undefined' for all that alerts : > > var b = [1,2,,4,5]; > alert(b[2]); Yes, reading a non-existing property yields the undefined value. This is no different from doing: var x = new Object(); alert(x.arglebargle); The object x has the property "arglebargle", so trying to read it yields undefined. > var c = [1,2,3,4,5]; > delete(c[2]) > alert(c[2]); Ditto. > var a = new Array(5); > alert(a[2]); .... > If an element of an array is not present > it is not 'null' > but only not known ('undefined'). If it's not present, it's just not present. It's not undefined (you can have properties present with the value undefined). If a property of an object exists, reading it returns its value. Howeve, if the property doesn't exist, reading it returns undefined, and taking the typeof of the property reference gives "undefined" as well. There is no special meaning to "null", it's just a value like any other. > var g = [0, 1, , 3, 4, 5]; > g[4] = null; > alert('arary length = '+g.length); > for(var i in g) > alert('g['+i+'] value = ' + g[i] + > '\nnull or undefined ? = ' + (g[i]==undefined||g[i]==null)) Should alert for indices 0, 1, 3, 4 and 5. Using g[i]==undefined||g[i]==null is equivalent to just writing g[i]==undefined since == equates null and undefined. If you want to be exact, you should write g[i] === undfined || g[i] === null /L -- Lasse Reichstein Holst Nielsen 'Javascript frameworks is a disruptive technology'
From: SAM on 10 Mar 2010 13:32 Le 3/10/10 5:35 PM, Lasse Reichstein Nielsen a �crit : > SAM <stephanemoriaux.NoAdmin(a)wanadoo.fr.invalid> writes: > >> var g = [0, 1, , 3, 4, 5]; >> g[4] = null; >> alert('arary length = '+g.length); >> for(var i in g) >> alert('g['+i+'] value = ' + g[i] + >> '\nnull or undefined ? = ' + (g[i]==undefined||g[i]==null)) > > Should alert for indices 0, 1, 3, 4 and 5. > Using > g[i]==undefined||g[i]==null > is equivalent to just writing > g[i]==undefined > since == equates null and undefined. > If you want to be exact, you should write > g[i] === undefined || g[i] === null Yes Jorge made like that and it is certainly better but I tried both with same results, so ... -- sm
From: Dr J R Stockton on 11 Mar 2010 13:08 In comp.lang.javascript message <g4WdnZdchaHfHArWnZ2dnUVZ8i6dnZ2d(a)bright view.co.uk>, Wed, 10 Mar 2010 11:23:46, Tim Streater <timstreater(a)waitrose.com> posted: >Let's say I have an array, say: > >var myarray = new Array (); > >Some time later, I may need to know about myarray(27) as follows: > >1) whether it's never yet been created >2) I previously used it but then set it to null You could also set it to the value undefined. You could also remove it by setting the length of the array to be short enough that it falls off the end. You could also, in at least one browser, delete it with the delete operator (check legality). >if (1) or (2) is true then I want to take some action. > >Can I test that in the following way: > >if (typeof(myarray[27])=="undefined" || myarray[27]==null) > { > doactions ... > } > >Googling appears to indicate that if I happen to have created >myarray[30], then the "if" statement above would work fine, but I >haven't found an indication of the situation if the highest index in >myarray that I had so far created were, say, 5. If the nature of the work is such that you will never assign "", false, or 0 to an entry, then testing with if (A[N]) or if (!A[N]) should suffice. Googling for programming questions will find you the right answer, a choice of wrong answers, and what looks like an unbounded amount of incomprehensible material. Better to read the ECMA standard (via FAQ & below), which will contain the right answer, no wrong answers, and a clearly bounded amount of incomprehensible material. It's a good idea to read the newsgroup c.l.j and its FAQ. See below. -- (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE8 FF3 Op10 Sf4 Cr4 news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>. <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: FireFoc support forums (Re: FF3.6 Stack output from Too MuchRecursion) Next: PASSING VALUES BETWEEN SCRIPTS |