On Feb 6, 7:36 am, ColinFine <colin.f...@pace.com> wrote:
> On Feb 5, 9:29 pm, Michael <mich...@vermontsnows.com> wrote:> Thank you all - 
> Kangax's low profile technique works like a champ.
>
> > Walter Lee: Regular expressions hurt my head. I will get there one
> > day.
>
> > The actual thing I am working on take about 100 alpha/numeric
> > sequences of no real logical order... so making it in regular
> > expression would take far longer (for me) then juts stubbing the 100
> > sequences in there.
>
> In Perl, I would not use regular expressions for this at all, but a
> hash:
>
> (equivalent of)
> var hash = {V30000: true, B47242:true,  V54000:true};
>
> function checkValid(value)
> {
>   return hash[value];}
>
> (or 'return hash[value] == true' if you're picky).
>
> This is a good idea in Perl because the language is optimised for
> accessing hash values: I don't know how true that is in javascript.

I'm pretty sure property lookup is faster than regexp in Javascript
too. The only downside to this approach is that there's no reliable
(native) "hash" structure in Javascript. There also needs to be a
boolean type conversion in your example, so that the function
consistently returns either `true` or `false` (and not, say,
`undefined` - for missing members). Regular property lookup is
unreliable in such way that it takes object's prototype chain into
account; That means there could be a conflict with
`Object.prototype.*` properties - e.g.: `isValid('toString')` would
return `Object.prototype.toString` which is a Function object and just
like any object - type converts to `true` : ) In more generic method,
it would probably be a good idea to use
`Object.prototype.hasOwnProperty`, but in this case, strict equality
operator would do:

var isValid = (function(){
  var hTable = { "V3000": true, "B47242": true, "V54000": true }
  return function(value) {
    return hTable[value] === true;
 // return Object.prototype.hasOwnProperty.call(hTable, value);
  }
})();

--
kangax
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to