Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign uptypeof([]) is object #982
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
gskachkov
Aug 27, 2017
I think it will not be changed, but you can use Object.prototype.toString.call:
Object.prototype.toString.call([]);
// "[object Array]"
gskachkov
commented
Aug 27, 2017
|
I think it will not be changed, but you can use Object.prototype.toString.call: Object.prototype.toString.call([]);
// "[object Array]" |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
leobalter
Aug 27, 2017
Member
an array is an exotic object in JS. It's still in an object, and as other built-ins we can roughly say its primitive type is an object, but also an instance of the Array constructor.
JS is not really versioned in its most important environments - Browsers - so any change here would break the web as so many code already rely on this operation and the respective return.
There are other ways to verify the instance, like instanceof or Object.getPrototypeOf. The typeof is not the one to proper check for other built-in or custom Objects.
|
an array is an exotic object in JS. It's still in an object, and as other built-ins we can roughly say its primitive type is an object, but also an instance of the Array constructor. JS is not really versioned in its most important environments - Browsers - so any change here would break the web as so many code already rely on this operation and the respective return. There are other ways to verify the instance, like |
leobalter
closed this
Aug 27, 2017
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
nexinspiration
Aug 27, 2017
@gskachkov There are a plenty of alternatives which are unstraightforward but IMO a better alternative like typeOf should be implemented
nexinspiration
commented
Aug 27, 2017
|
@gskachkov There are a plenty of alternatives which are unstraightforward but IMO a better alternative like typeOf should be implemented |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
leobalter
Aug 27, 2017
Member
@gskachkov: (...) a better alternative like typeOf should be implemented
why? Isn't Object.getPrototypeOf reasonable enough to identify the immediate constructor?
why? Isn't |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
nexinspiration
commented
Aug 27, 2017
|
@leobalter Its not straight forward and typeOf is more readable. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
nexinspiration
Aug 27, 2017
@leobalter I understand deprecating or changing typeof breaks a lot in the web. I am just stating a more readable alternative to be provided for the future IMO.
nexinspiration
commented
Aug 27, 2017
|
@leobalter I understand deprecating or changing typeof breaks a lot in the web. I am just stating a more readable alternative to be provided for the future IMO. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
robpalme
commented
Aug 27, 2017
|
Array.isArray is the straight forward and reliable way. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
claudepache
Aug 27, 2017
Contributor
For the specific case of arrays, there is Array.isArray()
For other objects, there are the instanceof operator, the constructor property, etc.
If you want to propose yet another way to identify object type, the proper venue is the es-discuss mailing list, as stated in https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#feature-requests
|
For the specific case of arrays, there is For other objects, there are the If you want to propose yet another way to identify object type, the proper venue is the es-discuss mailing list, as stated in https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#feature-requests |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
nexinspiration
Aug 27, 2017
@robpalme Yes Array.isArray() looks like a more direct way but is that a proper alternative when you wanna check and verify types at places? For example it's pretty straight in other languages
@claudepache Thanks for the info. Will do it.
nexinspiration
commented
Aug 27, 2017
|
@robpalme Yes Array.isArray() looks like a more direct way but is that a proper alternative when you wanna check and verify types at places? For example it's pretty straight in other languages @claudepache Thanks for the info. Will do it. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
dcy0701
Aug 28, 2017
So I think it should be deprecated or not recommended using the typeof operator because it does not make sense.
dcy0701
commented
Aug 28, 2017
|
So I think it should be deprecated or not recommended using the |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
ljharb
Aug 28, 2017
Member
It makes perfect sense - an array is an object. The only object typeof behaves differently for is functions, because of the [[Call]] internal slot.
I'd ask a better question: why do you have an API that can accept a value that's either "an array, or not"? If you expect an array, use Array.isArray() - and throw or default if you don't get one.
What's the possible use case for where you need to use typeof to generically determine the type of something, where you care that it's an array, but you also don't care whether it's a Date, RegExp, Map, Set, Promise, Object.create(null), HTMLElement, instance of any userland constructor, or boxed primitive?
|
It makes perfect sense - an array is an object. The only object I'd ask a better question: why do you have an API that can accept a value that's either "an array, or not"? If you expect an array, use What's the possible use case for where you need to use |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
nexinspiration
Aug 28, 2017
@ljharb I perfectly agree that Array.isArray() is good but when you come from other programming languages where you use typeof determine if its an array or not. It is weird when you get it as an object in javascript. I consider it as a matter of learning. I get that there is a workaround but is that the right way is my question.
nexinspiration
commented
Aug 28, 2017
|
@ljharb I perfectly agree that Array.isArray() is good but when you come from other programming languages where you use typeof determine if its an array or not. It is weird when you get it as an object in javascript. I consider it as a matter of learning. I get that there is a workaround but is that the right way is my question. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
ljharb
Aug 28, 2017
Member
It certainly is a matter of learning, but "language X isn't like language Y" isn't necessarily a flaw in either language.
If you have a use case where you think having typeof return "array" is necessary, I'd love to hear it - I've never seen one.
|
It certainly is a matter of learning, but "language X isn't like language Y" isn't necessarily a flaw in either language. If you have a use case where you think having |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
nexinspiration
Aug 28, 2017
@ljharb As I said it is not a matter of use case, hence am not pursuing this further. I was just making a statement for the ease of usage by people. It just doesn't feel its right to have array and null having typeof as object.
For Example: https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript
check this discussion on how people are confused.
nexinspiration
commented
Aug 28, 2017
|
@ljharb As I said it is not a matter of use case, hence am not pursuing this further. I was just making a statement for the ease of usage by people. It just doesn't feel its right to have array and null having typeof as object. For Example: https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript check this discussion on how people are confused. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
bathos
Aug 30, 2017
Contributor
typeof null being object is understood to be a bug, but its long history has made it hard to fix. on the other hand typeof [] being object is consistent with the language’s value model. With the exception of the null/function quirks, think of typeof as "what primitive type is this?". Array is not a primitive type in ES, rather it’s a subclass of Object. The distinction between types of objects (meaning: from what does this object inherit?) is generally done with instanceof. The prototype chain for arrays is at minimum Object > Array.
[] instanceof Array; // true
[] instanceof Object; // true
|
|
nexinspiration commentedAug 27, 2017
typeof([]) is object.
This should return array. There should be a deprecation of the current typeof and an alternative by name like typeOf to be considered.
Reason: This poses a confusion for the new devs and generally its array in other languages.