Benefit of using Object.hasOwnProperty vs testing if Property is undefined

泪湿孤枕 提交于 2019-12-03 16:32:07

问题


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.

For example:

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.

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]);
    };
}

回答1:


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.

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")
}

On jsfiddle




回答2:


The hasOwnProperty method checks that property is assigned to object directly. So, if property 'a' is in prototype hasOwnProperty will filter that.

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

if (obj.hasOwnProperty('a')) { /* code does not work */ }
if (obj.a !== undefined) { /* code works */ }

So, hasOwnProperty is safer in many cases.




回答3:


hasOwnProperty does not check for undefined values only checks if property is asigned to the object even if is undefined

var obj = { a : undefined }; 
obj.hasOwnProperty("a") //true 
obj.a === undefined     //true
obj.hasOwnProperty("b") //false
obj.b === undefined     //true   


来源:https://stackoverflow.com/questions/17150396/benefit-of-using-object-hasownproperty-vs-testing-if-property-is-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!