Created
December 1, 2010 12:17
-
-
Save stagas/723408 to your computer and use it in GitHub Desktop.
typeof alternative
This file contains 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
// typeof alternative | |
var check = function() { | |
var args = Array.prototype.slice.call(arguments) | |
, o = args[0] | |
, a = args.slice(1) | |
if (typeof o !== 'undefined' && (typeof o[a[0]] !== 'undefined') || !a.length && typeof o !== 'undefined') | |
if (a.length > 1) { | |
b = o[a[0]] | |
a.shift() | |
a.unshift(b) | |
return check.apply(this, a) | |
} | |
else return true | |
else return false | |
} | |
var x = { y: { z: 'foo' } } | |
, u | |
console.log(check()) // false | |
console.log(check(x)) // true | |
console.log(check(x, 'y')) // true | |
console.log(check(x, 'y', 'z')) // true | |
console.log(check(x, 'y', 'z', 'k')) // false | |
console.log(check(u)) // false | |
console.log(check(u, 'a')) // false | |
console.log(check(x, 'a')) // false | |
console.log(check(x, 'y', 'a')) // false | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment