我在 ie8 上使用 angular-1.2.1.js 并收到错误:
对象不支持属性或方法“hasOwnProperty” (在 Angular ForEach 方法内)
function forEach(obj, iterator, context) {
var key;
if (obj) {
if (isFunction(obj)){
for (key in obj) {
if (key != 'prototype' && key != 'length' && key != 'name' && obj.hasOwnProperty(key)) {
iterator.call(context, obj[key], key);
}
}
} else if ...
根据这个 stackoverflow post , hasOwnProperty 在 ie8 中不起作用(至少在 window 对象上不起作用)
我不确定事情是否表现得很奇怪,因为我在 Windows 8 ie 上使用 ie8 模式,但是 hasOwnProperty 在我使用时有效
var a = {b:'c'}
a.hasOwnProperty('b') //true
为什么 ie8 会抛出错误,我该如何解决? 谢谢
最佳答案
我也遇到了这个错误,并在 angular.js Github Source 上找到了这个编辑来解决这个问题:
if (obj) {
if (isFunction(obj)){
for (key in obj) {
//- if (key != 'prototype' && key != 'length' && key != 'name' && obj.hasOwnProperty(key)) {
//+ // This uglyness around hasOwnProperty is for IE8 to work properly (hasOwnProperty doesn't exist)
/*+*/ if (key != 'prototype' && key != 'length' && key != 'name' && (obj.hasOwnProperty && obj.hasOwnProperty(key) || Object.prototype.hasOwnProperty.call(obj, key))) {
iterator.call(context, obj[key], key);
}
}
还有一项编辑可以让 console.log 在 IE8 上工作 https://github.com/pjparra/angular.js/commit/bcbf9409f10f5988f6946a7b0381eee5e6518989
来源:https://github.com/pjparra/angular.js/commit/8c2ed24412620d68a760cfab70e4dc27a49b9e91
关于javascript - Angular ie8 "Object doesn' t支持属性或方法 'hasOwnProperty'“解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20500730/