Core JavaScript 1.5 Reference:Global Objects:Boolean
From MDC
目录 |
[编辑] 摘要
Core Object
Boolean 对象是布尔值的容器。
[编辑] 构造器
Boolean 构造器:
new Boolean(value)
[编辑] 参数
-
value - The initial value of the
Booleanobject. The value is converted to abooleanvalue, if necessary. If value is omitted or is 0, -0, null, false,NaN, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object or the string "false", create an object with an initial value of true.
[编辑] 说明
仔细区分Boolean值与Boolean对象的一些区别。
任何一个值不是undefined、null的对象出现在条件语句中时,结果为真(即使是值为false的Boolean对象)。 下面这个例子中,条件的结果为真:
x = new Boolean(false); if (x) //条件为真true
这个行为与Boolean值的结果不同。例如,下面的条件结果为假false:
x = false; if (x) //条件为假false
不要使用Boolean对象来将非Boolean值转化成Boolean值,应该使用Bolean 函数来执行这一过程:
x = Boolean(expression); //正确 x = new Boolean(expression); //不要这样用
如果将一个对象(即使是一个值是flash 的 Boolean对象)传递给Boolean对象的构造,那么该Boolean对象的值是true。
myFalse = new Boolean(false); // initial value of false
g = new Boolean(myFalse); //initial value of true
myString = new String("Hello"); // string object
s = new Boolean(myString); //initial value of trueb
不要在应该使用Boolean值的地方使用Boolean对象。
[编辑] Backward 兼容性
[编辑] JavaScript 1.2 and earlier versions
The Boolean object behaves as follows:
- When a
Booleanobject is used as the condition in a conditional test, JavaScript returns the value of theBooleanobject. For example, aBooleanobject whose value is false is treated as the primitive value false, and aBooleanobject whose value is true is treated as the primitive valuetruein conditional tests. If theBooleanobject is afalseobject, the conditional statement evaluates tofalse.
- You can use a Boolean object in place of a Boolean primitive.
[编辑] 属性
构造器: Specifies the function that creates an object's prototype.
prototype: Defines a property that is shared by all Boolean objects.
[编辑] 方法
toSource: Returns an object literal representing the specified Boolean object; you can use this value to create a new object. Overrides the Object.toSource method.
toString: Returns a string representing the specified object. Overrides the Object.toString method.
valueOf: Returns the primitive value of a Boolean object. Overrides the Object.valueOf method.
In addition, this object inherits the watch and unwatch methods from Object.
[编辑] 例子
[编辑] 创建初始值为false假的Boolean对象
bNoParam = new Boolean();
bZero = new Boolean(0);
bNull = new Boolean(null);
bEmptyString = new Boolean("");
bfalse = new Boolean(false);
[编辑] 创建初值为true真的Boolean对象
btrue = new Boolean(true);
btrueString = new Boolean("true");
bfalseString = new Boolean("false");
bSuLin = new Boolean("Su Lin");