Monday, May 16, 2011

JavaScript Object Type Checking

So today's tip is for a simple function that can be used to confirm if something is of an object type or not.

The Code:
function isObject(a) {
  return Object(a) === a;

The Explanation:
When you pass in a argument into the standard "Object" javascript object (like so "Object(a)"), internally it will return the original argument (in this case "a") IF its indeed an object.

And the === offer a "type safe" comparison to confirm if your argument is an object.

Hope that makes sense, if you have any questions please comment and I'll help out.

No comments:

Post a Comment

Fork me on GitHub