NaN is creepy and I may have to include it. Using undefined kind of defeats the purpose because [][0] is undefined, and so is [undefined][0]. So there's no way around it without something like hasOwnProperty(), which I don't believe was intended for such use and I don't think works on arrays anyway. MMMmmm. It does work on arrays:
Code:
[undefined].hasOwnProperty(0)
is true. Unfortunately:
Code:
[].hasOwnProperty(0)
is a syntax error. But you can do:
Code:
a = []
[]
a[1] = 'bob'
"bob"
a.hasOwnProperty(0)
false
a[0] = undefined
undefined
a.hasOwnProperty(0)
true
So, combined with length, you could work something out. But what I'm working with should be OK as long as someone doesn't use undefined and is trying to mean false when the default is true:
Code:
var runningflags = {};
function lotto(cfg){
cfg = cfg || {};
var defaults = {
totalnumbers: 10, // how many numbers to generate
lowerbound: 1, // lower bound for generated numbers
upperbound: 70, // upper bound for generated numbers
interval: 20, // how quickly calculations repeat until finalized (milliseconds)
caltime: 1000, // how long until calculations finalized (milliseconds)
id: 'lottery1', // id of target element that shows result
noadjacent: true, // should there be at least 2 between selected numbers
padsingle: false, // should single digits be padded with preceding 0
sort: true, // should result be sorted
dupes: false // allow duplicate numbers
};
this.setprops(defaults, cfg); . . .
and:
Code:
lotto.prototype = {
setprops: runningflags.hasOwnProperty? function(d, c){for(var p in d){this[p] = c.hasOwnProperty(p)? c[p] : d[p];}
} : function(d, c){for(var p in d){this[p] = c[p] || [0, '', null, false].indexOf(c[p]) > -1? c[p] : d[p];}}, . . .
This will allow lowerboud to be set in the cfg to 0 and things like noadjacent and sort in the cfg to be set to false, even when .hasOwnProperty() is unavailable. Now I know that often, if not aways, when .hasOwnProperty() is unavailable that [].indexOf() is also unavailable. But that's easy to fix:
Code:
if(![].indexOf){ // for IE 8 and less
Array.prototype.indexOf = function(v, i){
var l = this.length;
if(typeof i === 'number'){if(i < 0){i = l + i;}} else {i = 0;}
--i;
while(++i < l){if(v === this[i]){return i;}}
return -1;
}
}
(taken loosely from the MDN Polyfill for Array.indexOf)
Maybe they have a poly for hasOwnProperty() - nope, just checked.
Bookmarks