r/javascript • • Jun 22 '16

Those return values though

https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/canPlayType
376 Upvotes

75 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Jun 22 '16

[deleted]

2

u/Ginden Jun 22 '16

You mistake Boolean used as constructor (bad) and Boolean used as function (coerces value to boolean; useful for Array.prototype.filter)

1

u/namesandfaces Jun 22 '16

Explain?

-1

u/Buckwheat469 Jun 22 '16

0

u/namesandfaces Jun 22 '16 edited Jun 22 '16

I don't think you've read the docs or the specs carefully. There's a difference between the Boolean constructed object and the function. The function performs type conversion.

const testBoolean = Boolean("");                            // used function, not object constructor
testBoolean ? console.log("true") : console.log("false");   // => "false"

0

u/Buckwheat469 Jun 22 '16

So not only are you down voting me for pointing out a place where Boolean object instantiation behaves differently than expected but you claim that I didn't read the docs, then you show a completely different usage pattern that works as intended. The point is that the Boolean object behaves in unpredictable manners depending on implementation, and for new developers that are prone to making simple mistakes, like using 'new' when they shouldn't, they should stick to the simpler methods instead, such as using !! for booleans.

0

u/namesandfaces Jun 22 '16

The Boolean function works as intended, and if browsers are following the ES specification, then there's a definition for both the object and function. Otherwise, whatever browser version you're talking about has not implemented ecmascript. You say that Boolean() behaves unpredictably based on implementation. What browser version are you talking about?

This "usage pattern" (using the function) is the prescribed one by the Mozilla docs, and the Mozilla docs distinguishes between the object constructor and the function. As does the specs. It works exactly as intended. You're the one who insists on framing the discussion around the Boolean object constructor.

The !! pattern is worse than the Boolean function because type conversion should be done explicitly and without extraneous effects. The reason why people do double-negation is because they only wanted type conversion, but the ! operator converts type and negates.

http://stackoverflow.com/a/1406618/5050271

0

u/Buckwheat469 Jun 22 '16

You seem to be reading too much into it. Try to read that first code example in the MDN again. It uses the new operator. Wouldn't that be confusing to a new developer looking for examples? I think so.