Le 20/01/2012 00:54, Brendan Eich a écrit :
David Bruant <mailto:bruan...@gmail.com>
January 19, 2012 1:15 AM
Le 19/01/2012 02:27, Waldemar Horwat a écrit :
Brendan: Kill typeof null. Replace it with Ojbect.isObject?
What would be the semantics of this?
It was not obvious but the precedent stems from the strawman that led
to my proposal to change typeof null:
http://wiki.ecmascript.org/doku.php?id=strawman:object_isobject&rev=1295471005
This week we considered the draft spec:
Object.isObject = function isObject(value) {
return typeof value === 'object'&& value !== null;
}
to be deficient because a function is also an object, so one might
rather have
Object.isObject = function isObject(value) {
return (typeof value === 'object'&& value !== null) || typeof
value == 'function';
}
That would be perfect (for me at least).
------
Object.isObject(null); // false
Object.isObject({}); // true
// so far so good :-)
Object.isObject(function(){}) // ?
------
I'd like to advocate "true" for the last case. For now, the best way
to test if something is of type Object (as defined in ES5.1 - 8.6, so
including function) is to do "o === Object(o)" (an alternative being
"o !== null && (typeof o === 'object' || typeof o === 'function')",
which is rather long and I have not seen much) which is a bit hacky
and not straightforward to read for those who are not familiar with
this trick.
If an Object.isObject is introduced, I'd be interested in seeing it
covering the 8.6 definition.
Or maybe introduce another function for this?
That came up too: Object.type(x) would be the new typeof. But it will
take a while to get adoption, it's not built-in so monkey-patchable etc.
If Object.isObject has the second definition you showed, I don't think
an Object.type will be necessary, because every type will be testable in
one "instruction". Strings, numbers, booleans have typeof, undefined and
null are unique values (testable with ===) and Object.isObject will test
for ES5.1 - 8.6 definition of objects. It won't be consistent as an
Object.type method would be, but as far as I'm concerned, I don't care.
David
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss