如果未定义属性,则使用Object.hasOwnProperty进行测试的好处 [英] Benefit of using Object.hasOwnProperty vs testing if Property is undefined

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

问题描述

由于 hasOwnProperty 有一些警告和怪癖(窗口/在ie8问题中广泛使用/等等).

Since hasOwnProperty has some caveats and quirks (window / extensive use in ie8 issues / etc).

我想知道是否有任何理由甚至使用它,并且是否只需测试一下属性是否未定义就更好了&更简单.

I was wondering if there is any reason to even use it, and if simply testing if a property is undefined is better justified & more simplistic.

例如:

var obj = { a : 'here' };

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

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

我只是想知道是否有人对此有深入的了解,所以我更喜欢使用跨浏览器最友好,最新的方法.

Just wondering if anyone has any good insight on this, I'd prefer to be using the most cross-browser friendly, and up to date methodology.

我也已经看到hasOwnProperty重写了这个原型,它可以工作,但是我并没有因为它的有用性而卖掉……

I've also seen this prototype over-write for hasOwnProperty, which works, but I'm not sold on it's 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的更多信息.据我所知,对于本机不支持polyfil hasOwnProperty的浏览器,没有很好的方法.我在野外看到了很多不同的结果,它们都会产生假阳性或阴性.如果我绝对没有其他选择,那么这就是我为自己使用而创建的内容,它也会遭受错误的肯定和否定.根据 MSDN .

As further information to the answer given by @Pavel Gruba, and the polyfil that you supplied. 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, it also suffers false positives and negatives. According to MSDN.

以下文档模式支持:怪癖,Internet Explorer 6 标准,Internet Explorer 7标准,Internet Explorer 8 标准,Internet Explorer 9标准,Internet Explorer 10 标准. Windows Store应用程序也支持.

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

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天全站免登陆