Created
July 27, 2012 15:08
-
-
Save karolk/3188558 to your computer and use it in GitHub Desktop.
checking 'deep' namespaces for existence and type
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
(function(host) { | |
host.is = function is(namespace, type) { | |
var ret = true, | |
parent = host, | |
chunks = namespace.split('.'); | |
chunks.forEach(function(e, i) { | |
if ( parent === null ) { //this has to be straight equality check, can't lookup properties on null | |
ret = false | |
return //break | |
} | |
if ( typeof parent[e] != 'undefined' || (i == chunks.length-1 && type == 'undefined') ) { | |
parent = parent[e] | |
} | |
else { | |
ret = false | |
return //break | |
} | |
}); | |
if (type) { | |
typeof parent == type || (ret = false); | |
} | |
return ret; | |
}; | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: