Is the hasOwnProperty method in JavaScript case sensitive?

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;
};

Leave a Comment