Javascript’s RegExp Has an Identity Crisis
I came across something strange while working on a javascript library for defining classes, creating inheritance relationships and testing inheritance relationships. Click the buttons and observe the output:
So is a RegExp a function or not? In the Firefox browser, typeof says yes. In all browsers, instanceof says no. Common sense says RegExp can be called like a function, so it should be considered an instance of both Function and RegExp (I would think RegExp should inherit Function). I plan on addressing this issue in my library (which still needs a catchy name so I can stop calling it “a javascript library for defining classes, creating inheritance relationships and testing inheritance relationships”).
July 31st, 2007 at 10:08 pm
Common sense doesn’t tell me that a regular expression can be called as a function. In, e.g., Opera, it isn’t a function, and calling it as one gives an error. Opera is completely compliant with ECMA 262 on this.
In Firefox, calling a regular expression as a function is equivalent to calling the “exec” method on the RegExp object, i.e., it’s a redundant feature. I guess it’s an old Netscape JavaScript leftover.
(Just came by to look at the 3d graphics. Nice!