Skip to content

Instantly share code, notes, and snippets.

@itarato
Created November 24, 2014 11:42
Show Gist options
  • Save itarato/ea0dfe58d77a6774ca23 to your computer and use it in GitHub Desktop.
Save itarato/ea0dfe58d77a6774ca23 to your computer and use it in GitHub Desktop.
Property chain check
"use strict";
/**
* Verify if all sub properties exist of an object.
*
* @example
* For example to verify if window.class.foo.bar.x exist:
* propertyChainExist(window, ['class', 'foo', 'bar', 'x']);
*
* @param object
* Start object.
* @param properties
* Array of string property names.
* @returns {boolean}
*/
var propertyChainExist = function ( object, properties ) {
var current_object = object;
var property;
while (property = properties.shift()) {
if (!current_object.hasOwnProperty(property)) {
return false;
}
current_object = current_object[property];
}
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment