Is hasOwnProperty()
method case-sensitive? Is there any other alternative case-insensitive version of hasOwnProperty
?
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).
Is Javascript Case Sensitive? JavaScript is a case-sensitive language. This implies that the language keywords, variables, operate names, and the other identifiers should be typewritten with an identical capitalization of letters.
JavaScript is Case Sensitive All JavaScript identifiers are case sensitive.
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.
Yes, it's case sensitive (so obj.hasOwnProperty('x') !== obj.hasOwnProperty('X')
) You could extend the Object prototype (some people call that monkey patching):
Object.prototype.hasOwnPropertyCI = function(prop) {
return ( function(t) {
var ret = [];
for (var l in t){
if (t.hasOwnProperty(l)){
ret.push(l.toLowerCase());
}
}
return ret;
} )(this)
.indexOf(prop.toLowerCase()) > -1;
}
More functional:
Object.prototype.hasOwnPropertyCI = function(prop) {
return Object.keys(this)
.filter(function (v) {
return v.toLowerCase() === prop.toLowerCase();
}).length > 0;
};
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