javascript - How do I access the Object.prototype method in the following logic? -


i using following logic i18n string of given key.

export function i18n(key) {   if (entries.hasownproperty(key)) {     return entries[key];   } else if (typeof (canadarm) !== 'undefined') {     try {       throw error();     } catch (e) {       canadarm.error(entries['databuildi18nstring'] + key, e);     }   }   return entries[key]; } 

i using eslint in project. getting following error:

do not access object.prototype method 'hasownproperty' target object. 'no-prototype-builtins' error.

how change code resolve error ? don't want disable rule.

you can access via object.prototype:

object.prototype.hasownproperty.call(obj, prop); 

that should safer, because

  • not objects inherit object.prototype
  • even objects inherit object.prototype, hasownproperty method shadowed else.

of course, code above assumes that

  • the global object has not been shadowed or redefined
  • the native object.prototype.hasownproperty has not been redefined
  • no call own property has been added object.prototype.hasownproperty
  • the native function.prototype.call has not been redefined

if of these not hold, attempting code in safer way, have broken code!

another approach not need call be

!!object.getownpropertydescriptor(obj, prop); 

Comments