Skip to content

Instantly share code, notes, and snippets.

@rkatic
Created April 30, 2011 07:00
Show Gist options
  • Save rkatic/949493 to your computer and use it in GitHub Desktop.
Save rkatic/949493 to your computer and use it in GitHub Desktop.
isArguments
// 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;
};
})();
@rkatic
Copy link
Author

rkatic commented May 2, 2011

Just note that your versions will fail with null or undefined values, so you should do obj != null as first check.

@kflorence
Copy link

Yeah I forgot to put that in there on my initial post, but I added it in right afterwards. Doh!

https://gist.github.com/952521

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment