Skip to content

Instantly share code, notes, and snippets.

@jnbek
Forked from ryanflorence/hasProperty.js
Created November 14, 2011 17:10
Show Gist options
  • Save jnbek/1364472 to your computer and use it in GitHub Desktop.
Save jnbek/1364472 to your computer and use it in GitHub Desktop.
See if an object has a property
var has = function (obj, property) {
var tree = obj,
split = property.split('.'),
last = split.pop();
while (next = split.shift()) {
tree = tree[next];
if (tree === undefined) return false;
}
return tree[last] === undefined ? false : true;
};
console.assert( has(o, 'foo'), 'foo');
console.assert( has(o, 'foo.bar'), 'foo.bar' );
console.assert( !has(o, 'bar'), 'bar' );
console.assert( !has(o, 'foo.bar.baz'), 'foo.bar.baz' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment