Created
November 24, 2014 11:42
-
-
Save itarato/ea0dfe58d77a6774ca23 to your computer and use it in GitHub Desktop.
Property chain check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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