473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

portable dictionary class


Hi All,

Can you show me an example of a working dictionary object that is
portable? I would like to have something like:

d = new PortableDict()

d.setValue('a',12)
d.setValue('b',-3)

subd = new PortableDict()
d.setValue(subd,51)

d.hasKey('a') // true
d.hasKey('c') // false
d.hasKey(subd) // true
d.getValue('a') // 12

etc.

Alternatively, is there a programming scheme that allows me to assign
custom properties/attributes to DOM elements? I can do it with
FireFox, but not with IE - it throws an error telling that the given
attribute is not supported. Which is strange because AFAIK in
JavaScript you can freely modify objects, add attributes and methods
on the fly. But it looks like that in IE, HTML DOM elements are not
real JavaScript objects.

Thanks,

nagylzs

Aug 23 '07 #1
8 2944
nagylzs wrote:
Can you show me an example of a working dictionary object that is
portable? I would like to have something like:

d = new PortableDict()
`d' should be declared:

var d = ...
d.setValue('a',12) d.setValue('b',-3)

subd = new PortableDict() d.setValue(subd,51)

d.hasKey('a') // true d.hasKey('c') // false d.hasKey(subd) // true
d.getValue('a') // 12
Why, write it yourself. You can also use an object literal to define the
object (but since you need it portable, that would not be an option,
depending on the degree of portability). You can use type-converting tests
and typeof tests to determine whether the object has the property etc. That
is really beginner's knowledge. Read the FAQ: http://jibbering.com/faq/
Alternatively, is there a programming scheme that allows me to assign
custom properties/attributes to DOM elements? I can do it with FireFox,
but not with IE - it throws an error telling that the given attribute is
not supported.
It would be possible to encapsulate the element object reference in a
property of a user-defined object. That user-defined object would be
native, and so could have any number of user-defined properties.
Which is strange because AFAIK in JavaScript you can freely modify
objects, add attributes and methods on the fly.
That applies to native objects, not to host objects.
But it looks like that in IE, HTML DOM elements are not real JavaScript
objects.
Any DOM object is a host object, not "a real JavaScript object".
J(ava)Script/ECMAScript is only the used interface language. And some
DOMs are more "forgiving", a feature that should not be relied on.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Aug 23 '07 #2
d = new PortableDict()

`d' should be declared:

var d = ...
Unless I want it to be assigned to the Global Object ;-)
>
d.setValue('a',12) d.setValue('b',-3)
subd = new PortableDict() d.setValue(subd,51)
d.hasKey('a') // true d.hasKey('c') // false d.hasKey(subd) // true
d.getValue('a') // 12

Why, write it yourself.
Of course. In fact I already started to write it. However, my problem
is a very common problem. I was hoping that there is a very popular
open source JavaScript library that I could use. I do not want to re-
implement the wheel unless I have to.
That is really beginner's knowledge. Read the FAQ:http://jibbering.com/faq/
I will read that. :-)
Thank you,

nagylzs

Aug 23 '07 #3
nagylzs wrote:
>>d = new PortableDict()
`d' should be declared:

var d = ...

Unless I want it to be assigned to the Global Object ;-)
Variables should *always* be declared, of course within the desired context.
>>d.setValue('a',12) d.setValue('b',-3)
subd = new PortableDict() d.setValue(subd,51)
d.hasKey('a') // true d.hasKey('c') // false d.hasKey(subd) // true
d.getValue('a') // 12
Why, write it yourself.

Of course. In fact I already started to write it.
Then it would have been wise for you to mention that, and perhaps to post
some snippets of it for review.
However, my problem is a very common problem.
As common as homework assignments are.
I was hoping that there is a very popular open source JavaScript library
that I could use. I do not want to re-implement the wheel unless I have to.
What gave you the idea that this group is about how to use search engines?
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Aug 23 '07 #4
It looks like you're a surviving topdown coder :) Seriously, if you're
coming from a non-OO background (BTW, like me), perhaps the following
coding style looks familiar. Most common operations:

var string = 'three'
var numbers = new Object()

// 3 assignment methods
numbers['one'] = 'uno'
numbers.two = 'dos' // previous is better habit
numbers[string] = 'tres'
This would be perfect, except that "numbers" is a host object in my
case. :-(

Aug 23 '07 #5
I found out that the problem was the indexOf method. Cannot be used in
IE 6. So now I'm using this custom array:

function AdvArray() {
var that = this;
that = new Array(arguments);
that.indexOf = function(avalue) {
for (var i = 0; i<that.length ; i++ ){
if (that[i] == avalue) {
return i;
}
}
return -1;
}
return that;
}

Functionally it is okay but I do not like it. I'm going to store some
1000 elements in a dict and call indexOf() frequently. Is there a
better way to implement this? I'm thinking about using a hash table to
find the index but how?

Thanks,

nagylzs

Aug 23 '07 #6
nagylzs wrote:
I found out that the problem was the indexOf method. Cannot be used in
IE 6.
And not in Firefox < 1.5, Netscape < 9.0b1, Opera, Konqueror, Safari, ...
So now I'm using this custom array:

function AdvArray() {
var that = this;
that = new Array(arguments);
Won't work, because that new array will have a reference to arguments as
its only element. With that approach, you will have to use

that = new Array();
for (var i = arguments.length; i--;) that[i] = arguments[i];
that.indexOf = function(avalue) {
for (var i = 0; i<that.length ; i++ ){
for (var i = 0, len = that.length; i < len; i++ )
{
if (that[i] == avalue) {
This should be a non-converting equals: ===
return i;
}
}
return -1;
}
return that;
}
if (typeof Array.prototype.indexOf != "function")
{
Array.prototype.indexOf = function(...)
{
// ...
}
}

would be simpler and more efficient. You would have to watch for it at
the `in' operation, of course.
Functionally it is okay but I do not like it. I'm going to store some
1000 elements in a dict and call indexOf() frequently. Is there a
better way to implement this? I'm thinking about using a hash table to
find the index but how?
a.b or a["b"]

RTFM.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Aug 23 '07 #7
nagylzs wrote:
> numbers[string] = 'tres'

This would be perfect, except that "numbers" is a host object in my
case. :-(
See <46**************@PointedEars.de>

Please provide proper attribution next time.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Aug 23 '07 #8
a.b or a["b"]

It won't work because keys cannot be objects. Remember, I wanted a
dictionary where keys can be objects.
RTFM.
You don't need to help beginners. But if you do it, please don't be
rude. What you wrote about doctype and <scriptmissing "type"
attribute is not helping anybody especially other angry
comp.lang.javascript folks. Nobody starts programming by learning the
language reference before writing "Hello world".

You made very good points anyway, and I must thank you.

Aug 23 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: none | last post by:
or is it just me? I am having a problem with using a dictionary as an attribute of a class. This happens in python 1.5.2 and 2.2.2 which I am accessing through pythonwin builds 150 and 148...
4
by: brianobush | last post by:
# # My problem is that I want to create a # class, but the variables aren't known # all at once. So, I use a dictionary to # store the values in temporarily. # Then when I have a complete set, I...
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
4
by: NullQwerty | last post by:
Hi folks, I have a Dictionary which contains a string key and an object value. I want the object value to point to a property in my class and I want it to be by reference, so that later on I...
8
by: akameswaran | last post by:
I wrote up a quick little set of tests, I was acutally comparing ways of doing "case" behavior just to get some performance information. Now two of my test cases had almost identical results which...
6
by: GiJeet | last post by:
hello, I'm trying to use a dictionary as a class member. I want to use a property to get/set the key/value of the dictionary but I'm confused as how to use a dictionary as a property. Since there...
1
by: GVDC | last post by:
Example server-side JavaScript Web script, Dictionary class //Dictionary class, hash array unlimited length configurable speed/efficiency // printf("<html><body>"); printf("<b>Creating...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.