Skip to content

authContext.js hasOwnMethod calls always return false #1902

Description

@ElectricNroff

function hasOwnMethod (obj, methodName) {
return Object.prototype.hasOwnProperty.call(obj || {}, methodName) && typeof obj[methodName] === 'function'

For ES6 classes, when a method is defined inside a class, it's attached to the class's prototype, and not assigned as an own property on the instantiated object. Because Object.prototype.hasOwnProperty specifically checks for properties directly on the object itself, it will return false for any class methods.

For example, suppose I temporarily modify authContext.js to have a new function:

function hasOwnMethod2 (obj, methodName) {
  return obj != null && typeof obj[methodName] === 'function'
}

and then I temporarily modify isRequesterSecretariat to do this at the very beginning:

  if (hasOwnMethod(orgRepo, 'isSecretariatByShortName')) {
      console.log('found method via hasOwnMethod');
  }

  if (hasOwnMethod2(orgRepo, 'isSecretariatByShortName')) {
      console.log('found method via hasOwnMethod2');
  }

and then I make an API call that requires an isRequesterSecretariat check, such as POST /registry/org

Only "found method via hasOwnMethod2" is logged. The original function, hasOwnMethod, will never return a truthy value for any of the checks for existence of methods in ES6 classes.

It's possible that calls to hasOwnMethod are currently unreachable, but still hasOwnMethod is not accomplishing its purpose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Needs Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions