Re: Good and bad invocation, a.k.a. call a RegExp
So some things are banned: some because they don't work as expected, e.g. the with-statement, some because they have performance penalty, e.g. passing a string instead of a function to setTimeout.
/\s/("/ /") doesn't add functionality to the language, just another syntax, and its a not very well documented one.
Actually no one said that: "please do everyone a favor and just forget about it"
But ask yourself: Does this syntax make your code more clear? Or less?
And if less: Does it add functionality beyond better documented and wider understood alternatives?
--- In firstname.lastname@example.org, "robert.luxemburg" <robert.luxemburg@...> wrote:
> I was playing around with JSLint lately, and entered the following:
> var ss = " ///// "(" \/ ");
> JSLint returned: "Problem at line 1 character 10: Bad invocation",
> and Firebug threw a TypeError, since strings are not callable.
> So I shuffled these very same characters a bit, until I got:
> var s = /\s/("/ /"); // " "
> and JSLint claimed: "Problem at line 1 character 9: Bad invocation",
> even though the above is, if not valid JS, implemented in Firefox,
> Safari, Chrome and Opera, as syntactic sugar for /\s/.exec("/ /").
> See here for a lengthy discussion:
> It's definitely an obscure corner case, but shouldn't this be fixed
> in JSLint? Or added to the corresponding JS Style Guide? "Don't
> think you can call a RegExp, and if you find out that you actually
> can, please do everyone a favor and just forget about it..." ?
- At 10/27/2009 06:56 PM, robert.luxemburg wrote:
>var ss = " ///// "(" \/ ");May I ask why, if you're supplying the string as a hard-coded
>var s = /\s/("/ /"); // " "
literal, are you testing it with a regular expression? Don't you as
the scripter already know its value?
I realize that the question "Why would you ever do such a thing?" is
very different from "Is it legal syntax?" But it is a mystery to my
why someone would call up the RegExp engine just to find out whether
a known quoted string contains certain patterns. Is it because the
string is supplied by a server-side script and not by a human hand?
Or because you simply enjoy using regular expressions, or exploring
unusual syntax in your scripts?
Juniper Webcraft Ltd.