nagylzs said:
Here it is. It works on FireFox. It does not work on IE and it does
not throw up the "Error on page" message. :-(
<html>
The DOCTYPE declaration is missing.
The required `type' attribute is missing.
http://validator.w3.org/
function PortableDict() {
var that = this;
`that' is not required here, literally.
that.keys = new Array();
that.values = new Array();
It would be better if you used the key-value based architecture that is
already available through native objects (property - value). With one
property for the keys and another for the values, you have to implement
the connection always.
this.items = new Array();
that.hasKey = function(akey) {
return (that.keys.indexOf(akey)>=0);
}
that.hasValue = function(avalue) {
return (that.values.indexOf(avalue)>=0);
}
Array.prototype.indexOf() is not available before JavaScript 1.6 (Gecko
1.8b1+), and it is not available in JScript 5.x. Hence the error message in
IE, which probably says a little bit more than just "Error on page".
that.valueOf = function(akey) {
var idx = that.keys.indexOf(akey);
if (idx>=0) {
return that.values[idx];
} else {
return undefined;
}
}
`valueOf' is the name and identifier of a built-in method, inherited from
Object.prototype. It is unwise to use it for a user-defined object, unless
it does what the built-in method does: return the value of *calling object*.
that.keyOf = function(avalue) {
var idx = that.values.indexOf(akey);
^^^^^^^
if (idx>=0) {
return that.keys[idx];
} else {
return undefined;
}
}
See above.
that.add = function(akey,avalue) {
that.keys.push(akey);
that.values.push(avalue);
}
function Item(key, value)
{
this.key = key;
this.value = value;
}
PortableDict.prototype.add = function(akey,avalue)
{
this.keys.push(new Item(akey, avalue));
}
or
PortableDict.prototype.add = function(akey,avalue)
{
this.keys.push({akey: avalue});
}
See?
that.remove = function(akey) {
[...]
}
that.toString = function() {
[...]
}
}
Object.prototype.toString() is also a built-in method, but you overwrite it
in an acceptable way. However, it would be better (more efficient etc.) if
you defined all methods here as prototype methods, so that all PortableDict
objects inherit it from their prototype objects:
PortableDict.prototype.toString = function()
{
// ...
};
aso.
</script>
</head>
<body bgcolor="#ccccff">
Replace deprecated format attributes in favor of CSS. And take heed of
http://www.w3.org/QA/Tips/color
<span id="log"></span>
<script>
See above.
function print(s) {
var log = document.getElementById("log");
Methods of host objects should be feature-tested before being called:
http://www.jibbering.com/faq/faq_notes/not_browser_detect.html#bdFD
log.innerHTML = log.innerHTML + s.toString() + "<br>";
The proprietary innerHTML property is known to be error-prone and should
at least be feature-tested before being used. The toString() method is
implicitly called on string concatenation, you can as well concatenate `s'
and get the same result.
I must also be a newbie to search engines, because I could not find
any useful. I tried "javascript dictionary", "javascript hash object"
and similar things, before I wrote here.
"javascript faq OR guide OR reference" (or another combination thereof)
obviously did not come to mind.
HTH
PointedEars