我认为这很容易:
if(typeof(Array.push) == 'undefined'){
//not defined, prototype a version of the push method
// Firefox never gets here, but IE/Safari/Chrome/etc. do, even though
// the Array object has a push method!
}
它在 Firefox 中运行良好,但在 IE、Chrome、Safari、Opera 中却不行,它们使用此测试将本机 Array 对象的所有属性/方法返回为“未定义”。
.hasOwnProperty(prop) 方法仅适用于实例......所以它不起作用,但通过反复试验,我注意到它有效。
//this works in Firefox/IE(6,7,8)/Chrome/Safari/Opera
if(typeof(Array().push) == 'undefined'){
//not defined, prototype a version of the push method
}
使用此语法来确定Native Object / ~"JavaScript Class"~上是否存在属性/方法有什么问题,或者有更好的方法吗?