Felix Böhm wrote:
Hi guys,

just a first try for a proposal: Array.prototype.[filter/some/every] currently require a function as an argument. Most of the time, everything I want to do is check if they match a certain pattern. So what I end up writing is a RegExp that matches the pattern, and calling its test() method for every member of the array.

My proposal is to allow people to use a regular expression as an argument for the three methods. Current implementations throw an error when they don't get a function as an argument, so this won't break any (working) code.

An example:

["fOo","bAr"].filter(function(elem){ return /foo/i.test(elem); });

should be replaceable with

["fOo","bAr"].filter(/foo/i);

Firefox 3 era js shell:

js> ["fOo","bAr"].filter(/foo/i);
fOo
js> version()
180

However, the latest Firefox and js shells do not make RegExp instances callable:

js> ["fOo","bAr"].filter(/foo/i);
typein:5: TypeError: /foo/i is not a function
js> ["fOo","bAr"].filter(re=/foo/i, re.test.bind(re));
typein:6: TypeError: re is not a function
js> ["fOo","bAr"].filter(function(){return /foo/i});
["fOo", "bAr"]

We removed callable regexps because they violate ES3, as David-Sarah Hopwood pointed out (https://mail.mozilla.org/pipermail/es-discuss/2008-November/008008.html and related).


For non-string members of the array, the current behavior of the RegExp.prototype.test method (ie. converting the value to a string) applies.

Any reasons why this is a bad idea? (Or is it just my bad attitude? :D )

typeof nativeObject == "function" <=> nativeObject is callable

is violated by making regexps callable.

/be
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to