How do I check if an object has a property in Java

2018-12-31 02:10发布

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

Consider:

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

Is that the best way to do it?

标签: javascript
23条回答
何处买醉
2楼-- · 2018-12-31 02:53

There is method "hasOwnProperty" exists on object, but its not recommended to call this method directly because might be sometimes the object is null or some property exist on object like: { hasOwnProperty: false }

So better way would be:

// good
var obj = {"bar": "here bar desc"}
console.log(Object.prototype.hasOwnProperty.call(obj, "bar"));

// best
const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope.
console.log(has.call(obj, "bar"));

查看更多
大哥的爱人
3楼-- · 2018-12-31 02:55

Note: the following is nowadays largely obsolete thanks to strict mode, and hasOwnProperty. The correct solution is to use strict mode and to check for the presence of a property using obj.hasOwnProperty. This answer predates both these things, at least as widely implemented (yes, it is that old). Take the following as a historical note.


Bear in mind that undefined is (unfortunately) not a reserved word in JavaScript if you’re not using strict mode. Therefore, someone (someone else, obviously) could have the grand idea of redefining it, breaking your code.

A more robust method is therefore the following:

if (typeof(x.attribute) !== 'undefined')

On the flip side, this method is much more verbose and also slower. :-/

A common alternative is to ensure that undefined is actually undefined, e.g. by putting the code into a function which accepts an additional parameter, called undefined, that isn’t passed a value. To ensure that it’s not passed a value, you could just call it yourself immediately, e.g.:

(function (undefined) {
    … your code …
    if (x.attribute !== undefined)
        … mode code …
})();
查看更多
呛了眼睛熬了心
4楼-- · 2018-12-31 02:55

You can also use the ES6 Reflect object:

x = {'key': 1};
Reflect.has( x, 'key'); // returns true

Documentation on MDN for Reflect.has can be found here.

The static Reflect.has() method works like the in operator as a function.

查看更多
宁负流年不负卿
5楼-- · 2018-12-31 02:57

You can use the in operator to check if the property exists on an object:

x = {'key': 1};
alert("key" in x);

You can also loop through all the properties of the object using a for - in loop, and then check for the specific property:

for (prop in x) {
    if (prop == "key") {
        //Do something
    }
}

You must consider if this object property is enumerable or not, because non-enumerable properties will not show up in a for-in loop. Also, if the enumerable property is shadowing a non-enumerable property of the prototype, it will not show up in Internet Explorer 8 and earlier.

If you’d like a list of all instance properties, whether enumerable or not, you can use

Object.getOwnPropertyNames(x);

This will return an array of names of all properties that exist on an object.

Finally, you can use the typeof operator to directly check the data type of the object property:

if (typeof x.key == "undefined") {
    alert("undefined");
}

If the property does not exist on the object, it will return the string undefined. Else it will return the appropriate property type. However, note that this is not always a valid way of checking if an object has a property or not, because you could have a property that is set to undefined, in which case, using typeof x.key would still return true (even though the key is still in the object).

Update: You can check if a property exists by comparing to the undefined javascript property

if (x.key === undefined) {
    alert("undefined");
}

This should work unless key was specifically set to undefined on the x object

查看更多
浅入江南
6楼-- · 2018-12-31 02:58

Yes it is :) I think you can also do Object.prototype.hasOwnProperty.call(x, 'key') which should also work if x has a property called hasOwnProperty :)

But that tests for own properties. If you want to check if it has an property that may also be inhered you can use typeof x.foo != 'undefined'.

查看更多
登录 后发表回答