| re: DOM object assignment
On 20 Dec 2004 13:55:08 -0800, Peter Nofelt <pcnofelt@gmail.com> wrote:
I presume that Fred has helped with the original problem, so I'll
concentrate elsewhere.
[snip]
[color=blue]
> // Function class for providing psudo-hashtable functionality
> function CHashTable(){[/color]
I thought you might like to know that your hashtable is broken.
[color=blue]
> var m_arryHashTable = new Array();[/color]
The first thing to note is that the length property of an array object
only changes if you assign values to an "array index" - I'll explain what
that is in a moment.
As you seem to understand, when you use square brackets with any
object[1], the expression is used to look up a value. In fact, *every*
expression (even a numeric literal) is evaluated, converted to string, and
used as a property name. The only exception is with array objects.
When the internal [[Put]] method (used to assign to property values) of an
array is called, it performs a test on the property name. It attempts to
convert the (string) property name to a (32-bit) number, then back to a
string. If the number is less than (2^32)-1, and the string values before
and after the conversion match exactly, the property name is considered an
array index. So:
'10' -> 10 -> '10' '10' == '10' therefore an array index
'ff' -> NaN -> 'NaN' 'ff' != 'NaN' therefore not an array index
'01' -> 1 -> '1' '01' != '1' therefore not an array index
Any property name can be used with an array object, but only array
indicies will ever change the length property. The obvious impact of this
is that your hashSize method will fail entirely unless every key is
considered an array index - you have to track addition and removal
operations yourself. The other, not so obvious problem, is that you don't
distinguish between properties that might originate from the object
prototype, and those added as a key.
Every object has a predefined set of properties. Some implementations may
include more than others. It's quite feasible that code may call
hash.checkLookup('pop')
on your hashtable. In this instance, checkLookup would return true even
though the code may not have added its own key named 'pop' because array
objects have a pop method. There are two simple ways to lessen the impact
of this problem:
1) Use a "plain" object to hold the properties, not an array.
Objects have fewer predefined properties so there's less chance
of any name collisions. Moreover, as you're not using any array
features, its more efficient to use an object.
2) Mutate the key before using it. You could easily add a unique
sequence of characters, such as '_ ' (yes, the space is
intentional) to the property name. It's highly unlikely that an
implementation will expose properties like that.
There are several hashtable implementations in the archives of this group
that vary from the quick and dirty to the fully-featured and robust. See
<URL:http://groups.google.com/groups?q=group%3Acomp.lang.javascript+%22hash+tabl e%22+%7C+hashtable>.
[snip]
[color=blue]
> this.checkLookup = function(p_strLookupKey){
> if(m_arryHashTable[p_strLookupKey] === undefined)
> return false;
> else
> return true;
> }[/color]
return undefined !== m_arryHashTable[p_strLookupKey];
or
return 'undefined' != m_arryHashTable[p_strLookupKey];
is preferable.
[snip]
[color=blue]
> function CSortList(){
> var m_docHtmlOptions;
> var m_hash = new CHashTable();
> var m_array = new Array();[/color]
You might want to consider whether it's worth initialising these methods
as you're only going to overwrite them.
[color=blue]
> var m_num = new Number();[/color]
This is certainly unnecessary. Here you're creating an immutable Number
object with a value of zero.
var m_num = 0;
would be more efficient.
[color=blue]
> this.swapOldOptionsWithNew2 = function(p_hash, p_sort){
> var docHtmlOptionsTemp = document.createElement("options");[/color]
There's no such thing as an OPTIONS element.
[color=blue]
> for (var i = 0; i < p_sort.length; i++)
> docHtmlOptionsTemp.option[i] = p_hash.getLookupValue(p_sort[i])
> return docHtmlOptionsTemp
> }[/color]
As this method doesn't access any private data, it would be better to add
it to the prototype of CSortList. That goes for any method in a similar
situation.
[snip]
[color=blue]
> this.sendFormReference = function()[/color]
[snip]
[color=blue]
> var docHtmlOptionsTemp = new document.createElement("options");[/color]
Again, no OPTIONS element. Also, the new operator is unnecessary.
[snip]
[color=blue]
> <p><br>[/color]
You should use padding or a margin rather than a BR element.
Good luck,
Mike
[1] This could cover anything. Objects, functions and arrays are obvious
examples, but the other types - boolean, date, number, regexp, string -
display this ability, too.
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail. |