요약
Boolean
객체는 boolean 값을 감싸고 있는 객체입니다.
문법
new Boolean([value])
전달인자
value
Boolean
객체의 초기값.
설명
첫 번재 전달인자로 전달되는 값은 필요하다면 boolean 값으로 변환된다. 값이 없거나 0, -0, null, false, NaN
, undefined, 빈 문자열 ("")이라면 객체는 false로 초기화된다. 문자열 "false"를 비롯한 그 외의 다른 값이라면 객체는 true로 초기화된다.
Boolean 객체의 true와 false 값을 원래 Boolean 값인 true, false와 혼동하지 않길 바란다.
false 값을 가진 Boolean 객체를 비롯한 undefined나 null이 아닌 값을 가진 모든 객체는 조건문에서 true로 간주된다. 예를 들어 다음에서는 조건이 true다.
x = new Boolean(false); if (x) { // . . . 이 코드는 실행된다. }
이런 동작은 원래의 Boolean 값에서는 적용되지 않는다. 예를 들어 다음의 조건은 false다.
x = false; if (x) { // . . . 이 코드는 실행되지 않는다. }
boolean이 아닌 값을 boolean 값으로 변환하는 용도로 Boolean 객체를 사용하지 말기를 바란다. 대신에 그런 동작을 하는 함수를 이용하도록 한다.
x = Boolean(expression); // 추천 x = new Boolean(expression); // 비추천
false값을 가진 Boolean 객체를 포함한 어떤 객체를 이용해서 Boolean 객체를 초기화 하더라도 값은 true이다.
myFalse = new Boolean(false); // false 값으로 초기화 g = new Boolean(myFalse); // true 값으로 초기화 myString = new String("Hello"); // string 객체 s = new Boolean(myString); // true 값으로 초기화
원래의 Boolean 값을 이용해야 하는 곳에 Boolean 객체를 사용하지 않도록 조심해야 한다.
속성
For properties available on Boolean
instances, see Properties of Boolean instances.
- Boolean.length
- Length property whose value is 1.
Boolean.prototype
- 모든 Boolean 객체에서 이용되는 속성을 정의.
메소드
For methods available on Boolean
instances, see Methods of Boolean instances.
전역 Boolean 객체에는 메소드가 없다. 하지만, prototype chain으로 상속받은 몇몇 메소드는 있다.
Boolean
instances
모든 Boolean
instances Boolean.prototype
에서 상속받는다. 다른 것들처럼 instance의 속성과 메소드를 prototype 객체에서 받아 사용한다.
속성
Boolean.prototype.constructor
- Returns the function that created an instance's prototype. This is the
Boolean
function by default.
메소드
Boolean.prototype.toSource()
- Returns a string containing the source of the
Boolean
object; you can use this string to create an equivalent object. Overrides theObject.prototype.toSource()
method. Boolean.prototype.toString()
- Returns a string of either
"true"
or"false"
depending upon the value of the object. Overrides theObject.prototype.toString()
method. Boolean.prototype.valueOf()
- Returns the primitive value of the
Boolean
object. Overrides theObject.prototype.valueOf()
method.
예제
false 값으로 초기화한 Boolean
객체 만들기
var bNoParam = new Boolean(); var bZero = new Boolean(0); var bNull = new Boolean(null); var bEmptyString = new Boolean(""); varbfalse = new Boolean(false);
true 값으로 초기화한 Boolean
객체 만들기
btrue = new Boolean(true); btrueString = new Boolean("true"); bfalseString = new Boolean("false"); bSuLin = new Boolean("Su Lin");