Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

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

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

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

Consider:

(考虑:)

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

Is that the best way to do it?

(那是最好的方法吗?)

  ask by sheats translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(因此,简单地将属性检查减少为x.key typeof this[property]或更糟糕的是, x.key会给您完全误导的结果。)

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.

(如果您想知道一个对象是否物理上包含一个属性(它不是来自原型链上的某个位置),那么object.hasOwnPropertyobject.hasOwnProperty的方法。)

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.)

((在Safari的较早版本(2.0.1和更早的版本中不存在),但那些浏览器版本已很少使用。))

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.

(如果您要查找的是对象上具有可迭代的属性(当您遍历该对象的属性时,它将出现),则可以执行以下操作: prop in object将为您提供所需的效果。)

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

(由于可能需要使用hasOwnProperty ,并且考虑到可能需要回退方法,因此,我向您提供以下解决方案:)

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

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.

(上面是hasOwnProperty跨浏览器的工作解决方案,但有一个警告:无法区分原型和实例上具有相同属性的情况-只是假设它来自原型。)

You could shift it to be more lenient or strict, based upon your situation, but at the very least this should be more helpful.

(您可以根据自己的情况将其更改为宽容或严格,但是至少这应该会有所帮助。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...