1. Two ways to judge
Method 1:"key" in obj
, the result is false, indicating that it is not included; otherwise, it means that it is included
Method 2:("key")
, obj represents the object, the result is false represents not included; otherwise, it represents inclusion
2. The difference between the two methods
Both methods can be used to check if an object contains specified properties, but they have some differences.
-
“key” in obj:
- This method is used
in
Operators to check whether the attribute exists in the object. - If the object
obj
Or its prototype chain contains an attribute named "key", then returntrue
。 - If the object
obj
The property named "key" does not contain, and the property does not exist on its prototype chain, then returnfalse
。
- This method is used
-
(“key”):
- This method is used
hasOwnProperty
Method to check whether the object directly has the specified attribute. - If the object
obj
Directly owned by “key” Properties of,Then returntrue
。 - If the object
obj
If you do not directly have an attribute named "key", or if the attribute exists on its prototype chain, then returnfalse
。
- This method is used
The differences are as follows:
-
"key" in obj
Check whether the attribute exists in the objectobj
or in its prototype chain, not just the object itself. -
("key")
Check whether the attribute exists directly in the objectobj
, its prototype chain is not included.
3. Give an example to illustrate:
var obj = { key: "value" }; ("key" in obj); // Output: true(("key")); // Output: true ("toString" in obj); // Output: true (toString is the prototype method of Object)(("toString"));// Output: false
In this example,obj
The object directly has a property named "key", so both methods returntrue
. At the same time, becausetoString
The method exists inObject
The prototype of , so usein
Operator check"toString" in obj
Will returntrue
,but("toString")
returnfalse
, because this attribute does not belong directly toobj
The object's.
Therefore, the choice of using the method depends on your specific need for the existence of the attribute, whether you want to check the attributes on the object and its prototype chain, or just check the attributes of the object itself.
Attachment: JS determines whether the key in an array or object exists
Arrays and objects in JS are equivalent. To determine whether a key exists in an array (or whether the object contains a certain property), we may immediately think of using ary[key] == undefined to determine whether the key exists in this array or object, but this judgment is problematic because there may beary = {key:undefined};
The correct way should be:
(key); or (key);
In addition, when looping arrays or objects, you should use:
for(var key in ary) { (key+" : "+ary[key]); }
Summarize
This is the article about comparing the two methods of JS judging whether an object has a certain key. For more related JS judging that an object has a certain key, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!