[Mapbender-dev] Re: [Mapbender] #120: replace ["..."] with dot notation in JS

Christoph Baudson (WhereGroup) christoph.baudson at wheregroup.com
Wed Oct 17 03:37:59 EDT 2007


Thanks for pointing that out. We can't get rid of the bracket syntax 
entirely (f.e. it's needed in every in "for ... in" construct), I didn't 
think about that.

Let's go with Marc's suggestion; Dot notation in general and square 
brackets when dynamically adressing object attributes.

Marc Jansen schrieb:
> Hi Christoph,
>
> first: I would be happy with both dot or bracket notation :-)
>
> I was thinking about dynamically adding attributes:
>
> var attrName = 'humpty';
> var attrValue = 'dumpty';
>
> var ordinaryObject1 = {};
> ordinaryObject1[attrName] = attrValue;
> // ordinaryObject1 is now {"humpty":"dumpty"}
>
> var ordinaryObject2 = {};
> ordinaryObject2.attrName = attrValue;
> // ordinaryObject2 is now  {"attrName":"dumpty"}
>
>
> And what about attributes starting with a number (perhaps not nice or 
> even allowed, I'm not sure):
> ordinaryObject1["123test"] = 456;
> // ordinaryObject1 is now {"humpty":"dumpty","123test":456}
> ordinaryObject2.123test = 456; // fails
>
>
> You said "square brackets indicate it's an array, but it really is an 
> object.".
> Some might think so. Yet when there is an alphanumeric string 
> following, it should be clear that we are talking about an object.
>
> As for JSlint, you are right and the same behaviour is found in the 
> Eclipse-Plugin Aptana (perhaps even in JSEclipse, not sure here)... 
> they all suggest using dot notation.
>
> Maybe we should go with dots, and in the rare cases that we need the 
> functionality described above, change to brackets. Nice discussion 
> though. I wolud be interested in more opinions on this or are we just 
> "splitting hairs" on a total side-topic?
>
> Just my two cents,
>
> -- Marc
>
>
> Christoph Baudson (WhereGroup) schrieb:
>> Yes, at some points Mapbender uses arrays where objects should be 
>> used; this is definitely faulty.
>>
>> There are two reasons why I think the dot notation is superior
>>
>> 1) Using x = {} and then x["key"] is misleading: the square brackets 
>> indicate it's an array, but it really is an object.
>>
>> 2) JSLint recommends dot notation (and I am a blind follower)
>>
>> Marc, if you have any arguments pro brackets, please share them with 
>> us. I would like to see all sides to the issue.
>>
>> Thanks
>>
>> Christoph
>>
>> Marc Jansen schrieb:
>>> Hi Christoph,
>>>
>>> I thought that the root of the failure might be the (IMHO) incorrect 
>>> usage of arrays in Javascript. Correct me if I'm but AFAIK there are 
>>> no associative arrays within javascript. The purpose of these are 
>>> handles by objects. I know that object have some disadvantages -- 
>>> e.g. they are missing the length property.
>>>
>>> I personally favor the bracketed syntax, but thats a sole personal 
>>> preference.
>>>
>>>
>>> -- Marc
>>>
>>> Christoph Baudson (WhereGroup) schrieb:
>>>> Sorry for the last post. Ignore it. Too much copy and paste for any 
>>>> human to handle. Here's the version I intended to post
>>>>
>>>> -- 
>>>>
>>>> To illustrate the difference, an example
>>>>
>>>> Does not work with toJSONString()
>>>>
>>>>        currentLayer.layer_style[count] = [];
>>>>        currentLayer.layer_style[count]["name"] = styleName;
>>>>        currentLayer.layer_style[count]["title"] = styleTitle;
>>>>        currentLayer.layer_style[count]["legendurl"] = styleLegendUrl;
>>>>        currentLayer.layer_style[count]["legendurlformat"] = 
>>>> styleLegendUrlFormat;
>>>>
>>>> Does work
>>>>
>>>>        currentLayer.layer_style[count] = {};
>>>>        currentLayer.layer_style[count].name = styleName;
>>>>        currentLayer.layer_style[count].title = styleTitle;
>>>>        currentLayer.layer_style[count].legendurl = styleLegendUrl;
>>>>        currentLayer.layer_style[count].legendurlformat = 
>>>> styleLegendUrlFormat;
>>>>
>>>> May work, not tried (curly brackets in line 1)
>>>>
>>>>       currentLayer.layer_style[count] = {};
>>>>       currentLayer.layer_style[count]["name"] = styleName;
>>>>       currentLayer.layer_style[count]["title"] = styleTitle;
>>>>       currentLayer.layer_style[count]["legendurl"] = styleLegendUrl;
>>>>       currentLayer.layer_style[count]["legendurlformat"] = 
>>>> styleLegendUrlFormat;
>>>> _______________________________________________
>>>> Mapbender_dev mailing list
>>>> Mapbender_dev at lists.osgeo.org
>>>> http://lists.osgeo.org/mailman/listinfo/mapbender_dev
>>>>
>>>
>>> _______________________________________________
>>> Mapbender_dev mailing list
>>> Mapbender_dev at lists.osgeo.org
>>> http://lists.osgeo.org/mailman/listinfo/mapbender_dev
>>
>>
>
> _______________________________________________
> Mapbender_dev mailing list
> Mapbender_dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/mapbender_dev


-- 
***************************************
Where2B-Konferenz 
Die Lösungskonferenz der WhereGroup
am 29. November 2007 in Bonn
http://www.where2b-conference.com
***************************************
_______________________________________

W h e r e G r o u p GmbH & Co. KG

Siemensstraße 8
53121 Bonn
Germany

Christoph Baudson
Anwendungsentwickler

Fon: +49 (0)228 / 90 90 38 - 17
Fax: +49 (0)228 / 90 90 38 - 11
olaf.knopp at wheregroup.com
http://www.wheregroup.com
Amtsgericht Bonn, HRA 6788
_______________________________________

Komplementärin:
WhereGroup Verwaltungs GmbH
vertreten durch:
Arnulf Christl, Olaf Knopp, Peter Stamm
_______________________________________



More information about the Mapbender_dev mailing list