Not sure why hasOwnProperty()
seems to be missing from my object...
I'm getting data from an http post in expressjs3, like this:
someControllerFunction: function(req, res){ var data = req.body.loc; ... }
However if I do:
data.hasOwnProperty('test');
I get:
Object object has no method 'hasOwnProperty'
Perhaps I'm missing something obvious, but what?
(Node 10.5, Express 3.2.1)
Both also support ES6 symbols. So what's the difference between the two? The key difference is that in will return true for inherited properties, whereas hasOwnProperty() will return false for inherited properties.
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).
The hasOwnProperty() method returns true if the property is directly present in the object (not in its prototype chain). If an object is an Array, then the hasOwnProperty() method can check if an index is available (not empty) in the array.
The hasOwnProperty() method will check if an object contains a direct property and will return true or false if it exists or not. The hasOwnProperty() method will only return true for direct properties and not inherited properties from the prototype chain.
The object may not have Object.prototype
as its prototype.
This is the case if the object was created with...
var data = Object.create(null);
You could use...
Object.prototype.hasOwnProperty.call(data, 'test');
...to test if the property exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With