//第一个,boolean.constructor_函数的引用
// 创建布尔对象
var myBool = new Boolean(true);
// 访问构造函数
printl(myBool.constructor); // 输出: function Boolean() { [native code] }
// 确认构造函数的类型
printl(myBool.constructor === Boolean); // 输出: true
//第二个,boolean.toString___布尔转字符
// 创建一个 Boolean 对象
var boolTrue = new Boolean(true);
var boolFalse = new Boolean(false);
// 使用 toString() 方法转换为字符串
var strTrue = boolTrue.toString(); // "true"
var strFalse = boolFalse.toString(); // "false"
// 打印结果
printl("布尔值转换为字符串的结果:");
printl("true 转换为字符串: " + strTrue); // 输出: true 转换为字符串: true
printl("false 转换为字符串: " + strFalse); // 输出: false 转换为字符串: false
// 直接使用 Boolean 对象
var boolNum = new Boolean(1);
printl("布尔对象(1)转换为字符串: " + boolNum.toString()); // 输出: true
var boolStr = new Boolean("");
printl("布尔对象('')转换为字符串: " + boolStr.toString()); // 输出: false
//第三个,boolean.valueOf___对象的原始值
// 创建一个 Boolean 对象
var bool1 = new Boolean(1); // 非零数字会被视为 true
var value1 = bool1.valueOf(); // 返回 true
printl(value1); // 输出: true
// 创建一个 Boolean 对象,值为 0
var bool2 = new Boolean(0); // 0 被视为 false
var value2 = bool2.valueOf(); // 返回 false
printl(value2); // 输出: false
// 创建一个 Boolean 对象,值为 null
var bool3 = new Boolean(null); // null 被视为 false
var value3 = bool3.valueOf(); // 返回 false
printl(value3); // 输出: false
// 创建一个 Boolean 对象,值为 undefined
var bool4 = new Boolean(undefined); // undefined 被视为 false
var value4 = bool4.valueOf(); // 返回 false
printl(value4); // 输出: false
// 创建一个 Boolean 对象,值为字符串 "hello"
var bool5 = new Boolean("hello"); // 非空字符串被视为 true
var value5 = bool5.valueOf(); // 返回 true
printl(value5); // 输出: true
// 创建一个 Boolean 对象,值为字符串 ""
var bool6 = new Boolean(""); // 空字符串被视为 false
var value6 = bool6.valueOf(); // 返回 false
printl(value6); // 输出: false
//第四个,prototype_扩展一个方法
// 扩展 Boolean 的原型,添加一个名为 myColor 的方法
Boolean.prototype.myColor = function () {
if (this.valueOf() === true) {
this.color = "green"; // 如果布尔值为 true,设置颜色为绿色
} else {
this.color = "red"; // 如果布尔值为 false,设置颜色为红色
}
}
// 创建布尔对象
var a = new Boolean(1); // 创建时传入 1,相当于 true
a.myColor(); // 调用 myColor 方法
// 获取 color 属性
var b = a.color; // 结果为 "green"
printl(b); // 输出: green
// 再创建一个布尔对象
var c = new Boolean(0); // 创建时传入 0,相当于 false
c.myColor(); // 调用 myColor 方法
// 获取 color 属性
var d = c.color; // 结果为 "red"
printl(d); // 输出: red
//第五个,类型.判断类型 方法以及相关的例子
// 定义类型对象
const 类型 = {};
/**
* 判断变量数据是什么类型
* @param {任意} use
* @returns {字符串|null} 成功:返回对应类型单词,失败:null
*/
类型.判断类型 = function (use) {
switch (typeof (use)) {
case 'undefined':
return 'undefined';
case 'number':
return 'number';
case 'string':
return 'string';
case 'boolean':
return 'boolean';
default:
if (Array.isArray(use)) return 'Array';
if (Object.prototype.toString.call(use) === '[object Object]') return 'Object';
}
};
// 测试不同类型
var a; // undefined
var b = 1223; // number
var c = 'abcd'; // string
var d = true; // boolean
var e = [1, 2, 3]; // Array
var f = {a: 10, b: 20}; // Object
// 输出类型
printl(类型.判断类型(a)); // 'undefined'
printl(类型.判断类型(b)); // 'number'
printl(类型.判断类型(c)); // 'string'
printl(类型.判断类型(d)); // 'boolean'
printl(类型.判断类型(e)); // 'Array'
printl(类型.判断类型(f)); // 'Object'
1. boolean.constructor
函数的引用
var myBool = new Boolean(true);
printl(myBool.constructor); // 输出: function Boolean() { [native code] }
printl(myBool.constructor === Boolean); // 输出: true
这段代码创建了一个新的布尔对象 myBool
,并打印其构造函数。constructor
属性返回一个指向创建该对象的 Boolean
函数的引用。检查 myBool.constructor === Boolean
返回 true
,表明 myBool
是由 Boolean
函数创建的。
2. boolean.toString()
--- 布尔转字符
var boolTrue = new Boolean(true);
var boolFalse = new Boolean(false);
var strTrue = boolTrue.toString(); // "true"
var strFalse = boolFalse.toString(); // "false"
printl("布尔值转换为字符串的结果:");
printl("true 转换为字符串: " + strTrue); // 输出: true 转换为字符串: true
printl("false 转换为字符串: " + strFalse); // 输出: false 转换为字符串: false
在此部分,创建两个布尔对象 boolTrue
和 boolFalse
,然后通过调用 toString()
方法将它们转换成字符串。最后打印转换结果,展示了布尔值如何转化为字符串。
3. boolean.valueOf()
--- 对象的原始值
var bool1 = new Boolean(1); // 非零数字会被视为 true
var value1 = bool1.valueOf(); // 返回 true
printl(value1); // 输出: true
var bool2 = new Boolean(0); // 0 被视为 false
var value2 = bool2.valueOf(); // 返回 false
printl(value2); // 输出: false
// 并继续创建其他测试布尔对象
DiffCopyInsert
这段代码介绍了 valueOf()
方法,返回布尔对象的原始值。通过创建不同值的布尔对象(如 1、0 等),展示了如何获取其原始布尔值(true
或 false
)。
4. Boolean.prototype
扩展一个方法
Boolean.prototype.myColor = function() {
if (this.valueOf() === true) {
this.color = "green"; // 如果布尔值为 true,设置颜色为绿色
} else {
this.color = "red"; // 如果布尔值为 false,设置颜色为红色
}
}
var a = new Boolean(1); // 创建时传入 1,相当于 true
a.myColor(); // 调用 myColor 方法
var b = a.color; // 结果为 "green"
printl(b); // 输出: green
这里扩展了 Boolean
的原型,在其中添加了 myColor
方法。这个方法基于布尔对象的值设置颜色为 "green" 或 "red"。创建布尔对象 a
,调用 myColor
方法后,获取并打印颜色属性。
5. 类型.判断类型
方法以及相关的例子
const 类型 = {};
类型.判断类型 = function (use) {
switch (typeof (use)) {
case 'undefined':
return 'undefined';
case 'number':
return 'number';
case 'string':
return 'string';
case 'boolean':
return 'boolean';
default:
if (Array.isArray(use)) return 'Array';
if (Object.prototype.toString.call(use) === '[object Object]') return 'Object';
}
}
// 测试不同类型
var a; // undefined
var b = 1223; // number
var c = 'abcd'; // string
var d = true; // boolean
var e = [1, 2, 3]; // Array
var f = {a: 10, b: 20}; // Object
// 输出类型
printl(类型.判断类型(a)); // 'undefined'
printl(类型.判断类型(b)); // 'number'
printl(类型.判断类型(c)); // 'string'
printl(类型.判断类型(d)); // 'boolean'
printl(类型.判断类型(e)); // 'Array'
printl(类型.判断类型(f)); // 'Object'
这段代码定义了一个对象 类型
,并为其添加了一个 判断类型
方法,能够根据传入的参数返回对应的数据类型。通过多个测试变量,展示了如何使用这一方法来判断不同数据类型并输出结果。

总结
这些代码片段展示了 JavaScript 中布尔对象的用法、如何通过方法扩展原型、以及自定义如何判断类型的功能。将它们组合在一起,可以实现更复杂的操作和功能。一些具体的运行案例也清晰地证明了每个方法的效果及其返回值。