문제

I can't seem to find a clear explanation as to what the difference is between these two. I'd also like to point out that I don't really understand the difference between literals and values either.

Do boolean literals use the Boolean object?

도움이 되었습니까?

해결책

A literal is a value you literally provide in your script, so they are fixed.

A value is "a piece of data". So a literal is a value, but not all values are literals.

Example:

1; // 1 is a literal
var x = 2; // x takes the value of the literal 2
x = x + 3; // Adds the value of the literal 3 to x. x now has the value 5, but 5 is not a literal.

For your second part of the question you need to know what a primitive is. It's a little more complicated than this, but you can view them as "all types that aren't an object". Javascript has 5 of those, including boolean and number. So those aren't usually an object.

Why then can you still do (152).toString() in Javascript? This is because of a mechanism called Coercion (in other languages also called auto-boxing). When required the Javascript engine will convert between a primitive and its object wrapper, e.g. boolean and Boolean. Here is an excellent explanation of Javascript primitives and auto-boxing.

Not that this behaviour isn't really what you'd expect sometimes, especially with Boolean

Example:

true; // this is a `boolean` primitive
new Boolean(true); // This results in an object, but the literal `true` is still a primitive
(true).toString(); // The literal true is converted into a Boolean object and its toString method is called
if(new Boolean(false)) { alert('Eh?'); }; // Will alert, as every Boolean object that isn't null or undefined evaluates to true (since it exists)

다른 팁

Values are expressions that can't be evaluated any longer. That means, these are values:

  • x
  • 123
  • true
  • "asdqwe"

Now, literals are fixed value expressions. From the above list, the following are literals:

  • 123
  • true
  • "asdqwe"

So, x has a value but not fixed.

Answering the main question, booleans can only have two literals: false and true, and every boolean variable is a boolean value.

You will see this in college in a compilers or computer semantics course, but the wikipedia pages linked here are very good if you still don't understand the difference.

Compared values of !!x, Boolean(x), new Boolean(x).valueOf()

[true, false, null, undefined, 1, 0, NaN, Infinity, "true", "false", "", [], {}, new Boolean(false)]
.forEach(e => console.debug(
   [ !!e, Boolean(e), (new Boolean(e)).valueOf() ], e
))

// !!e, Boolean, valueOf
[true,  true,    true]   true
[false, false,   false]  false
[false, false,   false]  null
[false, false,   false]  undefined
[true,  true,    true]   1
[false, false,   false]  0
[false, false,   false]  NaN
[true,  true,    true]   Infinity
[true,  true,    true]   "true"
[true,  true,    true]   "false"
[false, false,   false]  ""
[true,  true,    true]   []
[true,  true,    true]   Object {}
[true,  true,    true]   Boolean {[[PrimitiveValue]]: false} // new Boolean(false)

Note:

Boolean(x) === !!x

typeof true == "boolean"
typeof Boolean(x) == "boolean"
typeof new Boolean(x) == "object" // not boolean!
typeof Boolean == "function"

Boolean(new Boolean(false)) == true // <- any object converted to boolean is true!
Boolean(new Boolean(false).valueOf()) == false
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top