New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typeof([]) is object #982

Closed
nexinspiration opened this Issue Aug 27, 2017 · 15 comments

Comments

Projects
None yet
8 participants
@nexinspiration

nexinspiration commented Aug 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.

@gskachkov

This comment has been minimized.

Show comment
Hide comment
@gskachkov

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]"
@leobalter

This comment has been minimized.

Show comment
Hide comment
@leobalter

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.

Member

leobalter commented Aug 27, 2017

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.

@leobalter leobalter closed this Aug 27, 2017

@nexinspiration

This comment has been minimized.

Show comment
Hide comment
@nexinspiration

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

@leobalter

This comment has been minimized.

Show comment
Hide comment
@leobalter

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?

Member

leobalter commented Aug 27, 2017

@gskachkov: (...) a better alternative like typeOf should be implemented

why? Isn't Object.getPrototypeOf reasonable enough to identify the immediate constructor?

@nexinspiration

This comment has been minimized.

Show comment
Hide comment
@nexinspiration

nexinspiration Aug 27, 2017

@leobalter Its not straight forward and typeOf is more readable.

nexinspiration commented Aug 27, 2017

@leobalter Its not straight forward and typeOf is more readable.

@nexinspiration

This comment has been minimized.

Show comment
Hide comment
@nexinspiration

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.

@robpalme

This comment has been minimized.

Show comment
Hide comment
@robpalme

robpalme Aug 27, 2017

Array.isArray is the straight forward and reliable way.

robpalme commented Aug 27, 2017

Array.isArray is the straight forward and reliable way.

@claudepache

This comment has been minimized.

Show comment
Hide comment
@claudepache

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

Contributor

claudepache commented Aug 27, 2017

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

@nexinspiration

This comment has been minimized.

Show comment
Hide comment
@nexinspiration

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.

@dcy0701

This comment has been minimized.

Show comment
Hide comment
@dcy0701

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 typeof operator because it does not make sense.

@ljharb

This comment has been minimized.

Show comment
Hide comment
@ljharb

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?

Member

ljharb commented Aug 28, 2017

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?

@nexinspiration

This comment has been minimized.

Show comment
Hide comment
@nexinspiration

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.

@ljharb

This comment has been minimized.

Show comment
Hide comment
@ljharb

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.

Member

ljharb commented Aug 28, 2017

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.

@nexinspiration

This comment has been minimized.

Show comment
Hide comment
@nexinspiration

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.

@bathos

This comment has been minimized.

Show comment
Hide comment
@bathos

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
Contributor

bathos commented Aug 30, 2017

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment