∟The "Boolean" Object Type - Wrapping Boolean Values into Objects
This section provides a quick description and a tutorial example script on the 'Boolean' built-in object type, which is used to create 'Boolean' objects.
The "Boolean" object type is a special built-in object type to be used to create a "Boolean" object,
which is different than a Boolean value of the Boolean primitive type.
It has the following main features:
New "Boolean" objects can be created with the "Boolean()" constructor. Note that "Boolean()" is also a regular function,
which returns a Boolean value, not a "Boolean" object.
All "Boolean objects have 2 inherited methods: toString() and valueOf().
A string value can be converted to a "Boolean" object on the fly with the (.) operator, for example: (2>1).toString().
The "Boolean" object type is used to wrap Boolean values into objects. There are no other benefits of using "Boolean" objects.
Here is a tutorial example script showing some of the those features:
// Boolean_Object_Type.html
// Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
// Creating a new Boolean object
var isBetter = new Boolean("JavaScript">"Java");
// Checking this object
println("\nAbout this \"isBetter\":");
println(" Type = "+(typeof isBetter));
println(" Instance Of Object: "+(isBetter instanceof Object));
println(" Instance Of Boolean: "+(isBetter instanceof Boolean));
// Using Boolean's inherited methods
var v = isBetter.valueOf();
var s = isBetter.toString();
println("\nExecution result:");
println(" typeof isBetter = "+(typeof isBetter));
println(" typeof isBetter.valueOf() = "+(typeof v));
println(" typeof isBetter.toString() = "+(typeof s));
println("\nConverting to Boolean objects on the fly:");
println(" \"...\".toString() = "
+(("JavaScript">"Java").toString()));
If you run this script with "jrunscript", you will get:
About this "isBetter":
Type = object
Instance Of Object: true
Instance Of Boolean: true
Execution result:
typeof isBetter = object
typeof isBetter.valueOf() = boolean
typeof isBetter.toString() = string
Converting to Boolean objects on the fly:
"...".toString() = true
Now we know how to create "Boolean" objects with the "Boolean()" constructor.