如何检查对象是否在JavaScript中具有属性? [英] How do I check if an object has a property in JavaScript?

查看:97
本文介绍了如何检查对象是否在JavaScript中具有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查对象是否具有JavaScript属性?

How do I check if an object has a property in JavaScript?

考虑:

x = {'key': 1};
if ( x.hasOwnProperty('key') ) {
    //Do this
}

这是最好的方法吗?

推荐答案

我真的很困惑的答案已经给出了 - 大多数都是完全不正确的。当然,您可以拥有具有undefined,null或false值的对象属性。所以简单地将属性检查减少到这个[property] 的类型,或者更糟糕的是, x.key 将完全给你误导性的结果。

I'm really confused by the answers that have been given - most of them are just outright incorrect. Of course you can have object properties that have undefined, null, or false values. So simply reducing the property check to typeof this[property] or, even worse, x.key will give you completely misleading results.

这取决于你在寻找什么。如果你想知道一个对象是否物理上包含一个属性(并且它不是来自原型链上的某个地方),那么 object.hasOwnProperty 就是要走的路。所有现代浏览器都支持它。 (它在旧版本的Safari中丢失 - 2.0.1及更早版本 - 但这些版本的浏览器很少再使用。)

It depends on what you're looking for. If you want to know if an object physically contains a property (and it is not coming from somewhere up on the prototype chain) then object.hasOwnProperty is the way to go. All modern browsers support it. (It was missing in older versions of Safari - 2.0.1 and older - but those versions of the browser are rarely used any more.)

如果您正在寻找什么for是否一个对象在其上有一个可迭代的属性(当你迭代对象的属性时,它会出现)然后执行:对象中的prop将给你你想要的效果。

If what you're looking for is if an object has a property on it that is iterable (when you iterate over the properties of the object, it will appear) then doing: prop in object will give you your desired effect.

因为使用 hasOwnProperty 可能就是你想要的,并且你可能想要一个回退方法,我向您提供以下解决方案:

Since using hasOwnProperty is probably what you want, and considering that you may want a fallback method, I present to you the following solution:

var obj = {
    a: undefined,
    b: null,
    c: false
};

// a, b, c all found
for ( var prop in obj ) {
    document.writeln( "Object1: " + prop );
}

function Class(){
    this.a = undefined;
    this.b = null;
    this.c = false;
}

Class.prototype = {
    a: undefined,
    b: true,
    c: true,
    d: true,
    e: true
};

var obj2 = new Class();

// a, b, c, d, e found
for ( var prop in obj2 ) {
    document.writeln( "Object2: " + prop );
}

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

if ( Object.prototype.hasOwnProperty ) {
    var hasOwnProperty = function(obj, prop) {
        return obj.hasOwnProperty(prop);
    }
}

// a, b, c found in modern browsers
// b, c found in Safari 2.0.1 and older
for ( var prop in obj2 ) {
    if ( hasOwnProperty(obj2, prop) ) {
        document.writeln( "Object2 w/ hasOwn: " + prop );
    }
}

以上是一个有效的跨浏览器解决方案到 hasOwnProperty ,但有一点需要注意:它无法区分原型和实例上相同属性的情况 - 它只是假设它来自原型。您可以根据自己的情况将其转移到更宽松或更严格的位置,但至少应该更有帮助。

The above is a working, cross-browser, solution to hasOwnProperty, with one caveat: It is unable to distinguish between cases where an identical property is on the prototype and on the instance - it just assumes that it's coming from the prototype. You could shift it to be more lenient or strict, based upon your situation, but at the very least this should be more helpful.

这篇关于如何检查对象是否在JavaScript中具有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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