Created
April 30, 2011 07:00
-
-
Save rkatic/949493 to your computer and use it in GitHub Desktop.
isArguments
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
// Cross-browser but not absolutely strong. | |
function isArguments( obj ) { | |
return typeof obj === "object" && ( "callee" in obj ) && typeof obj.length === "number"; | |
} | |
// Ideally strong, but broken on Opera... | |
var isArguments = (function( undefined ) { | |
var toStr = ({}).toString, | |
returnTrue = function() { | |
// To be sure it will not be inlined (future engines). | |
return arguments !== undefined; | |
}, | |
test = function( obj ) { | |
return obj != null && toStr.call( obj ) === "[object Arguments]"; | |
}; | |
return test( arguments ) ? test : | |
function( obj ) { | |
// Using "in" works in strict mode too. | |
if ( obj != null && "callee" in obj ) { | |
try { | |
return returnTrue.apply( this, obj ); | |
} catch (e) {} | |
} | |
return false; | |
}; | |
})(); |
Yeah I forgot to put that in there on my initial post, but I added it in right afterwards. Doh!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just note that your versions will fail with null or undefined values, so you should do
obj != null
as first check.