method-missing
v2.0.0
Published
Catch calls to missing methods on ES6 classes and objects.
Maintainers
Readme
Method Missing
Catch calls to methods that do not exist. Extend MethodMissing from your ES6 class (or base class), define __call(name, args), and every call to a missing method is handed to it. Small, with no dependencies.
It also works for static methods and plain objects.
MethodMissing is used in extends-classes, which allows a class to extend several classes.
Installation
$ npm install method-missingUsage
const MethodMissing = require('method-missing');
class Simple extends MethodMissing {
__call(name, args) {
return `The method '${name}' was called with: ${args.join(', ')}`;
}
}
const simple = new Simple();
simple.nonExistent('Hello!');
// The method 'nonExistent' was called with: Hello!__call receives the method name and an array of the arguments. Whatever it returns is returned to the caller, and this is the instance, so return this makes missing methods chainable.
Methods, properties and getters that do exist behave as normal.
Static methods
Wrap the class with MethodMissing.static and define a static __call:
class Simple {
static __call(name, args) {
return `The method '${name}' was called with: ${args.join(', ')}`;
}
}
const Wrapped = MethodMissing.static(Simple);
Wrapped.nonExistentStatic(1, 2, 3);
// The method 'nonExistentStatic' was called with: 1, 2, 3A class can do both: extend MethodMissing for its instances and be wrapped with MethodMissing.static for its static methods.
Objects
Pass a handler function as the second argument:
const object = MethodMissing.static({
one() {
return 'hey there';
}
}, (name, args) => `Sorry, method '${name}' doesn't exist.`);
object.one(); // hey there
object.two(); // Sorry, method 'two' doesn't exist.Changing the handler name
Pass the name to super, and to MethodMissing.static for static methods:
class Test extends MethodMissing {
constructor() {
super('missing');
}
missing(name, args) {
return `The method '${name}' was called with: ${args.join(', ')}`;
}
static missing(name, args) {
return `The static method '${name}' was called with: ${args.join(', ')}`;
}
}
const Wrapped = MethodMissing.static(Test, 'missing');What counts as missing
Reading any property that does not exist on the object returns a function that calls your handler, so typeof simple.anything is 'function'. Three kinds of lookup are left alone and return undefined, because the language makes them on every object:
then, so instances can be awaited, resolved by promises and returned fromasyncfunctions.toJSON, so instances can be passed toJSON.stringify.- Symbols, such as
Symbol.iterator.
Define any of these on your class and they work as usual.
If there is no handler (no __call on the class), missing properties are undefined, the same as on any object.
Upgrading from 1.x
- The value returned by the handler is now returned to the caller. In 1.x it was discarded.
then,toJSONand symbols are no longer handed to the handler. In 1.x this madeawait instancehang andJSON.stringify(instance)returnundefined.- A class with no handler no longer throws
MethodMissingErrorwhen a missing property is read; the property isundefined. - Getters now run with the instance, not the unwrapped object, as
this.
Tests
$ npm install
$ npm test