使用 Object.hasOwnProperty 与测试属性是否未定义的好处 [英] Benefit of using Object.hasOwnProperty vs. testing if a property is undefined

查看:66
本文介绍了使用 Object.hasOwnProperty 与测试属性是否未定义的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 hasOwnProperty 有一些警告和怪癖(窗口/广泛使用 Internet Explorer 8 问题等):

Since hasOwnProperty has some caveats and quirks (window / extensive use in Internet Explorer 8 issues, etc.):

是否有任何理由使用它?如果简单地测试一个属性是否未定义,是否更合理、更简单?

Is there any reason to even use it? If simply testing if a property is undefined, is it better justified and more simplistic?

例如:

var obj = { a : 'here' };

if (obj.hasOwnProperty('a')) { /* do something */ }

if (obj.a !== undefined) { /* do something */ }
// Or maybe (typeof (obj.a) !== 'undefined')

我更喜欢使用对跨浏览器最友好的最新方法.

I'd prefer to be using the most cross-browser friendly, and up to date methodology.

我也看到这个原型被 hasOwnProperty 覆盖了,它可以工作,但我并没有因为它的用处而卖力...

I've also seen this prototype overwritten for hasOwnProperty, which works, but I'm not sold on its usefulness...

if (!Object.prototype.hasOwnProperty) {
    Object.prototype.hasOwnProperty = function(prop) {
        var proto = this.__proto__ || this.constructor.prototype;
        return (prop in this) && (!(prop in proto) || proto[prop] !== this[prop]);
    };
}

推荐答案

作为给出答案的进一步信息 由 Pavel Gruba 提供,以及您提供的 polyfil:

As further information to the answer given by Pavel Gruba, and the polyfil that you supplied:

据我所知,对于原生不支持 hasOwnProperty 的浏览器,没有什么好的方法可以对其进行 polyfil.我在野外见过很多不同的,它们都会产生假阳性或阴性.如果我绝对别无选择,那么这就是我为我的用途而创建的,但它也会遭受误报和误报.根据 MSDN.

To the best of my knowledge, there is no good way to polyfil hasOwnProperty for browsers that do not support it natively. I have seen quite a few different ones in the wild and they all produce false positives or negatives. If I have absolutely no alternative then this is what I created for my use, but it also suffers false positives and negatives. According to MSDN.

支持以下文档模式:Quirks、Internet Explorer 6标准、Internet Explorer 7 标准、Internet Explorer 8标准、Internet Explorer 9 标准、Internet Explorer 10标准.Windows 应用商店应用也支持.

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards. Also supported in Windows Store apps.

JavaScript

function is(x, y) {
    if (x === y) {
        if (x === 0) {
            return 1 / x === 1 / y;
        }

        return true;
    }

    var x1 = x,
        y1 = y;

    return x !== x1 && y !== y1;
}

function hasOwnProperty(object, property) {
    var prototype;

    return property in object && (!(property in (prototype = object.__proto__ || object.constructor.prototype)) || !is(object[property], prototype[property]));
}

function NewClass() {}
NewClass.prototype = {
    a: 'there'
};

var obj = new NewClass();

if (obj.hasOwnProperty("a")) {
    console.log("has property")
}

if (hasOwnProperty(obj, "a")) {
    console.log("has property")
}

关于 JSFiddle.

这篇关于使用 Object.hasOwnProperty 与测试属性是否未定义的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆