-
Notifications
You must be signed in to change notification settings - Fork 163
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
clarify "is an object or function" #243
Comments
It means "If Type( The "or function" addendum was put there as a gentle reminder that all functions are objects as well, i.e. that the callability of Your (4) expression is an implementation of the predicate, but not more than that. There are many other possible implementations, and none should be used in the specification. |
@bergus, thanks for your reply and helpful links. I know your wikipedia link was at least half-joking, but I do want to point out in seriousness that there are at least 3 different meanings of "object" in the context of javascript, and it doesn't look to me like the ECMAScript 2105 spec is very successful at sorting them out.
Perhaps that's true, but it's not at all obvious to me, even after you pointed it out and after I've gone over sections 4 and 6 of the ECMAScript spec to which you referred. In particular, my (1) and (2) still look like pretty good guesses to me, aside from the fact that the compliance test suite flunks them (which could actually be a bug in the test suite, for all I know).
I'd be interested to see some of the "many other possible implementations" you have in mind, if you care to share. I only know of (4), and I got that by fumbling around in the dark til the test passed.
Can you say more about why you feel that none of them should be mentioned? From my point of view, spelling out one of them for definiteness sure sounds like a good thing-- it would have saved me a substantial amount of time and aggravation. |
Actually, my reading of Table 35 is that my (4) is not a valid test for the Object type after all, despite the fact that the compliance test suite passes it. The reason I believe (4) isn't valid is that, according to the last line of that table, if |
In fact, it looks to me like Table 35 completely nails down the relationship between Object-ness and the So, it looks to me like the following is a (the?) correct test for Object-ness:
Assuming I have that right so far, It may be that there is an alternative implementation that uses |
I had |
@bergus thanks for the example. Yes, it looks to me like
More material here: http://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript . In particular, Oriol correctly points out that my proposed test using Idiomatic (I guess that means "commonly used"?) or not, (4) is simply wrong according to the ECMAScript spec that you claim to be following.
That sentence really doesn't hold up very well at all. Don't you think it would be better to actually follow and enforce the definition of Object that you're citing, instead of coming up with reasons to do some other thing based on presumptions of what all pathological objects want and shaky logic? The definition is actually looking fairly clear to me now, to my surprise. I believe it's clear at this point that (4) simply fails to comply with the promises/A+ spec, In any case, I'd also strongly recommend addressing the original issue, as follows:
|
These objects you are talking about are ignored because they're basically non-existent. They are absolutely non-standard, and while the ES spec does permit them it explicitly discourages implementations from using them. The only examples that come to my mind are The other reason why |
@bergus, thanks, good to know all these things. Well, I guess flunking (4) isn't really an option then. In that case it would be great if the spec could be changed to match the suite. Not that I give a f*** about those objects, it's just that if there wasn't this apparent hole in the logic, it would have saved me from worrying or thinking much about it; likewise for others in my position in the future, and people out on the web who are trying to make sense of the spec. In chrome: Yeah, the lack of any efficient bulltetproof way to ask is-an-object is pretty bizarre. |
Actually... it looks like
So, as far as I know, neither nodejs nor chrome has any way of creating an object whose behavior is described by the last line of Table 35 and whose typeof doesn't return |
I second @donhatch that this should be specified. The definition of
The definition uses this language:
Yet how to correctly detect "is an object or function" is left unclear. Referencing abstract operations in the ECMAScript spec is reasonable, and if that's the intention it should be explicit. |
In "2.3.3. Otherwise, if
x
is an object or function",it's not clear what "is an object or function" means.
My first guess was this:
which seems to give the correct answer on values I tested it with.
However, apparently
x instanceof Function
impliesx instanceof Object
(at least on values I tested it with), so the following seemed sufficient:
However, the compliance test flunked that, complaining that my implementation
didn't examine
x.then
in some cases when it should have(namely when "
x
is an object with null prototype").So I tried switching from
instanceof
totypeof
,and after some experimenting came up with this:
But the compliance test flunked that too, complaining that my implementation
behaved wrongly on
x=null
.Finally I tried this:
And finally the compliance test passes.
I don't think this was time well spent,
nor do I think it's tenable for the compliance test
to be the arbiter of what the spec means.
If the spec means exactly (4), it would be better if it would say that.
The text was updated successfully, but these errors were encountered: