<ontopic :) > On Mon, Mar 19, 2012 at 3:03 PM, Dmitriy Sintsov questpc@rambler.ru wrote:
On 19.03.2012 17:23, Krinkle wrote:
On Mon, Mar 19, 2012 at 9:35 AM, Daniel Friesen lists@nadir-seen-fire.com**wrote:
On Mon, 19 Mar 2012 00:40:54 -0700, Dmitriy Sintsovquestpc@rambler.ru
wrote: var jqgmap = [];
for ( var mapIndex in jqgmap ) {
This is VERY bad JavaScript coding practice. Please use $.each().
This is rather exaggerated. Even more when looking at that suggestion.
Arrays should, indeed, not be enumerated with a for-in loop. Arrays in JS can only contain numeral indices, so they should simply be iterated with a simple for-loop like this `for (i = 0; i< myArr.length; i += 1) { .. }` (maybe cache length for slight performance gain by reducing property lookups).
My array is numeric but sparse,
myArr = []; myArr[0] = a; myArr[1] = b; So I cannot just use incremental key iteration, at least existence of element should be checked.
If you use an array, use `myArr[myArr.length] = value;` or `myArr.push(value);` to add something to it. And `.slice(..)` or `.splice(..)` to remove something from it. Never set a key directly or remove a key directly.
If you want a non-linear array, create an object instead. JavaScript allows to do this (because Array is just an extension of Object in javascript), but that doesn't mean you should.
If you need non-linear keys, don't create an array!
<code> var myObj = {}; // not [] myObj.property = value;
var customProp = getPropName(); myObj[customProp] = value;
for ( var key in myObj ) { // myObj[key] } </code>
-- Krinkle
On 19 March 2012 16:29, Krinkle krinklemail@gmail.com wrote:
<ontopic :) >
..
If you need non-linear keys, don't create an array!
<code> var myObj = {}; // not [] myObj.property = value;
var customProp = getPropName(); myObj[customProp] = value;
for ( var key in myObj ) { // myObj[key] }
</code>
-- Krinkle
Suppose you want to use indexOf in a array. http://www.cjboco.com/blog.cfm/post/indexof-problems-in-internet-explorer/
So, lets use the power of JS to fix JS, .... lets add indexOf to the prototype http://stackoverflow.com/questions/948358/array-prototype-problem
Ooops, Array.prototype is global
var a = [1,2,3,4,5]; for (x in a){ // Now indexOf is a part of EVERY array and // will show up here as a value of 'x' }
So what you do? you use a library for that. http://documentcloud.github.com/underscore/ http://api.jquery.com/jQuery.inArray/
$.each , _.each $.inArray, _.include( and a lot of other nice tools that make you happy.
People seems to think functional programming is not something to avoid, but something that can be usefull.
wikitech-l@lists.wikimedia.org