Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
zyklus opened this Issue · 11 comments

4 participants

@zyklus

See title :)

@michaelficarra

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.

@michaelficarra

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.

@zyklus

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.

@michaelficarra

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?

@zyklus

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

@michaelficarra

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?

@zyklus

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

@michaelficarra

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

@zyklus

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

@yahelc

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

https://bugs.webkit.org/show_bug.cgi?id=28285
http://code.google.com/p/v8/issues/detail?id=617
https://bugzilla.mozilla.org/show_bug.cgi?id=582717#c7

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

@timruffles
Collaborator

Any pull-requests arising from this would be welcome!

@timruffles timruffles closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.