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

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

问题描述

如何在 JavaScript 中检查对象是否具有特定属性?

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

考虑:

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

这是最好的方法吗?

推荐答案

我真的被给出的答案弄糊涂了 - 大多数答案完全不正确.当然,您可以拥有具有 undefined、null 或 false 值的对象属性.因此,简单地将属性检查减少到 typeof this[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.)

如果您要查找的是对象是否具有可迭代的属性(当您迭代对象的属性时,它会出现)然后执行:prop in object会给你想要的效果.

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