Get mp3 Michael Buble Pitbull MP3 search Gipsy Kings Nat King Cole All mp3 genres Coil Neil Young Suzy Solar Shakira Eva Cassidy Stevie Ray Vaughan Cobra Starship Useless Pickles » Blog Archive » Javascript’s RegExp Has an Identity Crisis

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”).

One Response to “Javascript’s RegExp Has an Identity Crisis”

  1. spoilspot Says:

    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! :)

Leave a Reply

You must be logged in to post a comment.