The Now Platform® Washington DC release is live. Watch now!
Yesterday I showed you how to represent things in a JavaScript object. But what if you have the opposite problem — you have an object, but you want to know what it represents? More precisely, you want to know if it has a particular property. Is there a way to do this in JavaScript?
Of course there is. Don't you know by now that there isn't anything that JavaScript can't do?
Suppose your writing a function raptoflivver that takes one argument, and you want to see if that argument has a property named zip. You could do something like this:
function raptoflivver(arg) {
if ('zip' in arg) {
// do stuff with zip, since we have the darned thing...
}
}
function raptoflivver(arg) {
if (arg.zip) {
// do stuff with zip, since we have the darned thing...
}
}
var zippo = {};
zippo.zip = null;
raptoflivver(zippo);
var zippo = {};
zippo.zip = false;
raptoflivver(zippo);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.