typeof( regex ) === 'object' in newer versions of V8 #112

zyklus opened this Issue Nov 21, 2011 · 11 comments


None yet

4 participants


See title :)


It's not like it has to, though. It can be either "object" or "function" depending on whether or not it implements [[Call]]. That's the idea that should get across.


Oh and your spacing is very misleading. typeof(X) === typeof (X) === typeof X. typeof is an operator and should be spaced like one. Like void or delete.


how is my spacing misleading?!? I know it's an operator, but wrapping it in parens prevents any confusion for new devs in even simple cases like:

typeof 1==2

the point of this is that in older versions of V8 typeof( /rx/ ) === 'function', in newer versions of V8 it is object. The site says that it is always function, just pointing out that that changed.


Well firstly developers need to either know their operator precedence or have a table on hand, but more importantly, your lack of spacing between the operator and your (needlessly) parenthesised expression appears to be a function invocation to many developers. You don't write return!a or typeof+a or throw"error", do you?


The entire point is that it looks like a function. The idea that "developers should just know everything" is crazy, no offense :) ( especially in this case. Do you really expect newbie devs to know if the above is doing typeof (1==2) or (typeof 1)==2, even with a reference?? )

typeof is unique in JS in that it's an op that acts in runtime as a function. For all effective purposes, for all 99% of devs care, it is a function. Yes, you can't do things like foo( typeof ) or typeof.call( bar ), but writing it as a function makes it immediately clear to anyone what the purpose is and it doesn't harm anything except a few electrons :)


typeof acts like a unary function just like unary +, unary !, and every other unary operator. void acts like a constant function, producing undefined on any input. Binary operators are just binary functions, but they can't be written that way like unary operators can. That's not an argument for writing purposely confusing syntax. There's no "right" or "wrong" here, I'm just trying to convince you not to conflate the ideas by writing them the same way. It's a best practices thing.

edit: as for precedence, this table should be easy for even novice JS users to memorise. There's nothing out of the ordinary if you've used any other language with infix operators. What next? we don't expect the average user to know how new works? Where would it end? What's the minimal set of realistic expectations?


but there aren't any unary ops that are words :) You can't write 5 plus 5 or not 42.

My whole point is this -- For someone that's familar with C/Java/PHP/etc, but not JS:

if( typeof foo === 'undefined' ) is confusing
if( typeof( foo ) === 'undefined' ) is clear

typeof is broken as is in every case except the one I just used, no reason to confuse people as well :)


but there aren't any unary ops that are words

typeof, new, void, delete. I assume you meant binary. In that case, in and instanceof are words that are binary operators.

typeof is broken

It's definitely not "broken". It just has very few legitimate use cases. Two, in fact. Safely test that a reference is declared with a comparison with "undefined" and test that a reference's value is callable with a comparison with "function". And now the conversation has been brought full circle.

edit added stuff


okay, so "broken" is somewhat of a loose term, how about "un-usable in ways that people would expect"? :) Unless you feel like sticking .valueOf() on everything :P


It looks like V8 and Nitro both removed support for callable RegExes; it landed in FF5, not sure when it landed in Chrome.


So, it doesn't seem like this is engine specific anymore so much as engine version specific.


Any pull-requests arising from this would be welcome!

@timruffles timruffles closed this Apr 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment