typeof null

Axel Rauschmayer axel at rauschma.de
Wed May 9 22:27:30 PDT 2012

Good points. In hindsight, my complaint was more about things currently being a bit confusing (one frequently reads that one shouldn’t use instanceof Array) than about proposing a specific solution. So the comment below helps.

On May 10, 2012, at 4:53 , Allen Wirfs-Brock wrote:

> The reason there will never be a single solution to these issue is that desirable behavior varies depending  upon what the programmer is trying to accomplish. Just using (possibly cross-frame) arrays an an example.  What is the programmer really asking when they say "is this object an Array".  Do the need to know whether it has array indexed properties and a length property? Do they need to know where it maintains the special Array length invariant? Do they need to know whether it implements it has properties that implement the array extra methods? Do they need to know that in inherits from the build-in array prototype object?  Or, do they just need to know whether they should serialize the object using [ ] instead of {  } notation?
> The last one was the situation that jason2.js ran into WRT cross-frame arrays and it provided the use case that lead to Array.isArray. But it isn't clear that was the best possible solution.  Another solution might have been to add a serializeAsArray property to Array.prototype.  That would work cross frame and it would have made it easier to for programmer to define there own internal abstractions that would also serialize using [ ] notation.
> Whenever anybody says they need to do a type test (or instanceof test) we need to ask them: Why? What is it you really need to know.  Smalltalkers learned fairly early that type/class testing was an anti-pattern. It makes code brittle and difficult to fix or extend. 
> I'm not sure what a cross-frame instanceof test (that didn't always return false) would mean. I understand why json2.js thought it needed to do a cross-frame isArray test.  I would like to understand other real use cases where cross-frame type testing is needed. Does anybody have any?

Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120510/1f81caa9/attachment.html>

More information about the es-discuss mailing list