- The Book of Dojo
- Quick Installation
- Hello World
- Debugging Tutorial
- Introduction
- Part 1: Life With Dojo
- Part 2: Dijit
- Part 3: JavaScript With Dojo and Dijit
- Part 4: Testing, Tuning and Debugging
- Part 5: DojoX
- The Dojo Book, 0.4
Checking Object Types
Submitted by criecke on Wed, 06/06/2007 - 21:23.
JavaScript's "instanceof" operator checks an object for class information. But there are nuances that make it difficult in practice. For example, a DOM NodeList can be accessed like an array with [] subscripts, but "instanceof Array" applied to a NodeList returns false nonetheless. These Dojo base functions shield those nasty details from you.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Type Checking Demo</title>
<script type="text/javascript"
src="http://o.aolcdn.com/dojo/1.0.0/dojo/dojo.xd.js"></script>
<script type="text/javascript">
//
dojo.addOnLoad(function() {
var allScripts = dojo.query("script");
var iceCreamObject = { flavor: "vanilla", scoops: 2};
// NodeLists are like arrays
console.debug(dojo.isArray(allScripts)); // True
// An arbitrary object. Arrays are objects too.
console.debug(dojo.isObject(iceCreamObject)); // True
console.debug(dojo.isObject(allScripts)); // True
console.debug(dojo.isFunction(dojo.query)); // True
console.debug(dojo.isFunction(dojo)); // False
// This next example hurts my head!
console.debug(dojo.isFunction(dojo.isFunction)); // True, but weird
// This is a common idiom for doing "nullable" arguments in the Dojo source code.
// Here, you can pass either myFn(var, function, var) or myFn(var, var);
var myFn = function() {
if (dojo.isFunction(arguments[1])) {
return arguments[1](arguments[0], arguments[2]);
} else {
return arguments[0]+arguments[1];
}
}
console.debug(myFn(1,2)); // 3
console.debug(myFn(1,Math.max,2)); // 2 = Math.max(1,2);
});
</script>
</head>
</html>
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Type Checking Demo</title>
<script type="text/javascript"
src="http://o.aolcdn.com/dojo/1.0.0/dojo/dojo.xd.js"></script>
<script type="text/javascript">
//
dojo.addOnLoad(function() {
var allScripts = dojo.query("script");
var iceCreamObject = { flavor: "vanilla", scoops: 2};
// NodeLists are like arrays
console.debug(dojo.isArray(allScripts)); // True
// An arbitrary object. Arrays are objects too.
console.debug(dojo.isObject(iceCreamObject)); // True
console.debug(dojo.isObject(allScripts)); // True
console.debug(dojo.isFunction(dojo.query)); // True
console.debug(dojo.isFunction(dojo)); // False
// This next example hurts my head!
console.debug(dojo.isFunction(dojo.isFunction)); // True, but weird
// This is a common idiom for doing "nullable" arguments in the Dojo source code.
// Here, you can pass either myFn(var, function, var) or myFn(var, var);
var myFn = function() {
if (dojo.isFunction(arguments[1])) {
return arguments[1](arguments[0], arguments[2]);
} else {
return arguments[0]+arguments[1];
}
}
console.debug(myFn(1,2)); // 3
console.debug(myFn(1,Math.max,2)); // 2 = Math.max(1,2);
});
</script>
</head>
</html>
- Printer-friendly version
- Login or register to post comments
- Subscribe post