by using the Boolean() function. The Boolean() function c an either be invoked
as a constructor (with the new operator) or as a conversion function (without the new
operator). In both cases its first step is to convert value to a b oolean value, where the
values 0, NaN, null , undefined, fal se, and the empty string "" are all converted
to false. All other values (primitive and object)—including the string "false"
are converted to true. After the conversion, the Bo olean() constructor returns a
Boolean object containing the converted value. The Boolea n() conversion function,
however, simply returns a primitive b oolean value.
Methods
toString()
Syntax:
boolean .toString()
Returns a string representation of the value stored in boolean , which can be either
"true" or "false". The method ca n also be invoked on a primitive boolean value, in
which case JavaScript creates a temporary wrapper Boolean object in or der to be able
to call the method. However, this is rarely needed in practice b ecause JavaScript calls
the toString() method automatically when the automatic conversion rules demand
that a boolean value be converted to a string (e.g., in a string con catenation with the
concatenation operator (+ )).
Examples:
var b = true;
var B = new Boolean(true);
B.toString() //Returns "true"
//In the following two examples JavaScript automatically creates
//temporary wrapper objects to be able to call toString():
b.toString() //Returns "true"
false.toString() //Returns "false"
valueOf()
Syntax:
boolean .valueOf()
Returns the primitive boole an value stored in the object.
E.5 console (Client-Side JavaScript)
Modern browsers have integrate d a debugging console (e.g., the JavaScript Console
in Google Chrome , or the Browser Console in Fir efox, both accessible through the
380 JavaScript Reference
JavaScript Reference
Ctrl+Shift+J shortcut) and imp le ment a Console object that is accessible through
the console global property. The Console object defines an API for basic debugging
tasks such as displaying dierent messages to a console window. Note that ther es no
formal standard defining the Console API, but vendors follow a de facto standard set
up b y the Firebug debugging extension of Firefox. While implementation s may dier
from vendor to vendor, support for the essential log() metho d is almost universal.
Methods
dir()
Syntax:
console.dir(object )
Displays object in the Console window. The pro grammer can examine the proper-
ties, me thods, o r elements of object , and inspect any nested objects interactively by
clicking disclosure triangles.
log()
Syntax:
console.log(obj1 , obj2 , ...,objN )
Outputs th e string rep resentations of its arguments to the console.
Consider, for example, the next code:
var t = "Hello there!";
var o = {a: 10, b: 20};
console.log(t, o);
Inside th e Console window, you can see the following information produced by the
above call to the log() method.
Observe how the string is displayed as normal text while the o object is displayed with
the names and values of both its properties.
You can also examine programmatically generated DOM elements in the Console
window. Take, for example, this code :
E.5. console (Cli ent-Side JavaScript) 381
var table = document.createElement("table");
var row = document.createElement("tr");
var cell = document.createElement("td");
cell.innerHTML = "Howdy!"
table.appendChild(row);
row.appendChild(cell);
console.log(table);
Feeding the code to the br owser produces the following situation.
Inspecting the Console window only shows you the <t able> element but you will
notice an additional triangle in front of it, which indicates that this item is expandable.
If you click it, the element expands a nd reveals the following structure.
time()
Syntax:
console.time(name )
The time() method starts a timer and assigns name to it. The timer’s name is later
used by the timeEnd() method to sto p the timer.
timeEnd()
Syntax:
console.timeEnd(name )
382 JavaScript Reference
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.10.107