Skip to content

Instantly share code, notes, and snippets.

@stagas
Created December 1, 2010 12:17
Show Gist options
  • Save stagas/723408 to your computer and use it in GitHub Desktop.
Save stagas/723408 to your computer and use it in GitHub Desktop.
typeof alternative
// 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