|
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.
cve-services/src/utils/authContext.js
Lines 43 to 44 in b88aebb
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:
and then I temporarily modify isRequesterSecretariat to do this at the very beginning:
and then I make an API call that requires an isRequesterSecretariat check, such as
POST /registry/orgOnly "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.