Boolean - JavaScript 编辑

The Boolean object is an object wrapper for a boolean value.

Description

The value passed as the first parameter is converted to a boolean value, if necessary. If the 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, an empty array ([]), or the string "false", create an object with an initial value of true.

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.

Any object of which the value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example, the condition in the following if statement evaluates to true:

var x = new Boolean(false);
if (x) {
  // this code is executed
}

This behavior does not apply to Boolean primitives. For example, the condition in the following if statement evaluates to false:

var x = false;
if (x) {
  // this code is not executed
}

Do not use a Boolean object to convert a non-boolean value to a boolean value. To perform this task, instead, use Boolean as a function, or a double NOT operator:

var x = Boolean(expression);     // use this...
var x = !!(expression);          // ...or this
var x = new Boolean(expression); // don't use this!

If you specify any object, including a Boolean object whose value is false, as the initial value of a Boolean object, the new Boolean object has a value of true.

var myFalse = new Boolean(false);   // initial value of false
var g = Boolean(myFalse);       // initial value of true
var myString = new String('Hello'); // string object
var s = Boolean(myString);      // initial value of true

Do not use a Boolean object in place of a Boolean primitive.

Note: When the non-standard property document.all is used as an argument for this constructor, the result is a Boolean object with the value false. This property is legacy and non-standard and should not be used.

Constructor

Boolean()
Creates a new Boolean object.

Instance methods

Boolean.prototype.toString()
Returns a string of either true or false depending upon the value of the object. Overrides the Object.prototype.toString() method.
Boolean.prototype.valueOf()
Returns the primitive value of the Boolean object. Overrides the Object.prototype.valueOf() method.

Examples

Creating Boolean objects with an initial value of false

var bNoParam = new Boolean();
var bZero = new Boolean(0);
var bNull = new Boolean(null);
var bEmptyString = new Boolean('');
var bfalse = new Boolean(false);

Creating Boolean objects with an initial value of true

var btrue = new Boolean(true);
var btrueString = new Boolean('true');
var bfalseString = new Boolean('false');
var bSuLin = new Boolean('Su Lin');
var bArrayProto = new Boolean([]);
var bObjProto = new Boolean({});

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'Boolean' in that specification.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:177 次

字数:7106

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文