In JavaScript, there are other ways to examine if an object has a selected property. The principle variations between these approaches, moreover efficiency and syntax, are the entry to personal prototypes or inherited properties from the prototype chain.
Any worth in javaScript, with the exception with undefined
and null
has a prototype chain, since primitive values will not be “by definition” an object, javaScript coerces or containers the primitive worth in a corresponding object. So the values has properties, strategies, constructor and prototype inherited from the prototype chain.
Among the subsequent methods contain checking each the properties immediately belonging to the article (personal properties) and people inherited from prototypes.
Utilizing in
Operator
The in
operator returns true if the article incorporates the property with specified identify. Checking in personal prototypes or its prototype chain (inherited properties)
code
const consumer = { identify: 'Alex', age: undefined }
const hasName = 'identify' in consumer
const hasSurname = 'surname' in consumer
const hasAge = 'age' in consumer
take a look at
take a look at('consumer ought to have identify property', () => {
anticipate(hasName).toBeTruthy()
})
take a look at('consumer mustn't have surname property', () => {
anticipate(hasSurname).toBeFalsy()
})
take a look at('consumer ought to have age property', () => {
anticipate(hasAge).toBeTruthy()
})
take a look at('consumer ought to have inherited properties', () => {
anticipate('valueOf' in consumer).toBeTruthy()
})
✅ PASS consumer ought to have identify property
✅ PASS consumer ought to NOT have surname property
✅ PASS consumer ought to have age property
✅ PASS consumer ought to have inherited properties
NOTE: In operator checks the property and never the worth assigned, for that reason returns true for age property even when is undefined.
🧐 Evaluating with undefined
A fast method to examine the if the article has a propierty is examine if the property is outlined. However since a property might have undefined as worth will not be a dependable method to examine.
Regardless of this drawback, this method has the benefit of getting the flexibleness to seek for properties inside nested objects with out throwing errors, making use of the non-obligatory chaining operator ?.
.
code
const consumer = {
identify: 'Alex',
age: undefined,
contact: { telephone: '+123' }
}
const hasName = consumer.identify !== undefined
const hasSurname = consumer.surname !== undefined
const hasAge = consumer.age !== undefined
const hasPhone = consumer.contact?.telephone !== undefined
const hasAddress = consumer.contact?.deal with !== undefined
take a look at
take a look at('consumer ought to have identify property', () => {
anticipate(hasName).toBeTruthy()
})
take a look at('consumer mustn't have surname property', () => {
anticipate(hasSurname).toBeFalsy()
})
take a look at('consumer ought to have age property', () => {
anticipate(hasAge).toBeTruthy()
})
take a look at('consumer ought to have telephone property', () => {
anticipate(hasPhone).toBeTruthy()
})
take a look at('consumer mustn't have deal with property', () => {
anticipate(hasAddress).toBeFalsy()
})
take a look at('consumer ought to have inherited properties', () => {
anticipate(consumer.valueOf !== undefined).toBeTruthy()
})
✅ PASS consumer ought to have identify property
✅ PASS consumer ought to NOT have surname property
❌ FAIL consumer ought to have age property
✅ PASS consumer ought to have telephone property
✅ PASS consumer ought to NOT have deal with property
✅ PASS consumer ought to have inherited properties
Utilizing Object.hasOwn(object1, 'prop')
methodology
The Object.hasOwn() static methodology returns true if the required object has the indicated property as its personal property. If the property is inherited, or doesn’t exist, the tactic returns false.
code
const consumer = { identify: 'Alex', age: undefined }
const hasName = Object.hasOwn(consumer, 'identify')
const hasSurname = Object.hasOwn(consumer, 'surname')
const hasAge = Object.hasOwn(consumer, 'age')
take a look at
take a look at('consumer ought to have identify property', () => {
anticipate(hasName).toBeTruthy()
})
take a look at('consumer mustn't have surname property', () => {
anticipate(hasSurname).toBeFalsy()
})
take a look at('consumer ought to have age property', () => {
anticipate(hasAge).toBeTruthy()
})
take a look at('consumer mustn't have inherited properties', () => {
anticipate(Object.hasOwn(consumer, 'valueOf')).toBeFalsy()
})
✅ PASS consumer ought to have identify property
✅ PASS consumer ought to NOT have surname property
✅ PASS consumer ought to have age property
✅ PASS consumer ought to NOT have inherited properties
NOTE: Even by overwriting the hasOwn methodology, the habits will not be affected.
🧐 Utilizing .hasOwnProperty()
methodology
Object.hasOwn() is meant as a substitute for this methodology.
Regardless of hasOwnProperty() methodology has higher browser assist there are two vital observations in regards to the habits:
-
hasOwnProperty() does not work for null-prototype objects, objects created like
const consumer = Object.create(null)
-
It does not work with objects which have overridden the inherited hasOwnProperty() methodology.
For this second state of affairs there a workaround utilizing the hasOwnProperty property from the Object prototypeObject.prototype.hasOwnProperty.name(myObj, 'myProp')
as a substitute ofmyObj.hasOwnProperty('myProp')
code
// null-prototype objects
const consumer = Object.create(null)
consumer.identify = 'Alex'
consumer.surname = 'Alex'
const hasName = consumer.hasOwnProperty('identify')
const hasSurname = Object.prototype.hasOwnProperty.name(consumer, 'surname')
take a look at
take a look at('consumer ought to have identify property', () => {
anticipate(hasName).toBeTruthy()
})
take a look at('consumer ought to have surname utilizing prototype', () => {
anticipate(hasSurname).toBeTruthy()
})
❌ FAIL consumer ought to have identify property
❌ FAIL consumer ought to have surname utilizing prototype
code
// hasOwnProperty overridden
const shopper = {
identify: 'A',
surname: 'B',
hasOwnProperty() { return false }
}
const hasClientName = shopper.hasOwnProperty('identify')
const hasClientSurname = Object.prototype.hasOwnProperty.name(shopper, 'surname')
take a look at
take a look at('shopper ought to have identify property', () => {
anticipate(hasClientName).toBeTruthy()
})
take a look at('shopper ought to have surname utilizing prototype', () => {
anticipate(hasClientSurname).toBeTruthy()
})
❌ FAIL shopper ought to have identify property
✅ PASS shopper ought to have surname utilizing prototype