以下是对于如何在JavaScript中检查对象是否为参数对象的进一步跟进。
所提供的解决方案都无法在IE8中运行。是否有办法在IE8中检测对象是否为参数对象?
@Rocket和@cliffs_of_insanity是正确的,我最初测试可能有误。
总之,我的结论是:
var isArguments = function(obj) {
return (obj != null) && // since undefined == null
((Object.prototype.toString.call(obj) == '[object Arguments]') ||
(!!obj.callee)); // fixes for ie8 non-strict-mode
};
关于arguments对象的历史和为什么它起作用的更多信息,请参见:https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments/callee
isArguments({callee: true})
等情况,所有浏览器都会返回 true
。 - Roman Pominov
callee
应该可以工作。 - gen_Eric.callee
测试,因为在严格模式下会抛出异常。return Object.prototype.toString.call(item)=== '[object Arguments]' || !! item.callee;
- cliffs of insanity