Since 2015, JavaScript has improved immensely.
It’s much more pleasant to use it now than ever.
In this article, we’ll look at properties in JavaScript.
hasOwnProperty()
SafelyhasOwnProperty
is a method of an object’s prototype.
Therefore, it can easily be overridden with an object’s own methods.
To call hasOwnProperty
safety, we can call it with call
.
So instead of writing:
obj.hasOwnProperty('prop')
We write:
Object.prototype.hasOwnProperty.call(obj, 'prop')
The 2nd way is safer because hasOwnProperty
is always part of the Object.prototype
.
We can’t guarantee that hasOwnProperty
isn’t overridden with the first way.
With ES6, we can use the Map
constructor to store key-value pairs, so that we don’t need to create objects to store them.
#programming #software-development #javascript #web-development #technology