50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
const path = require('path');
|
|
|
|
class This {
|
|
constructor() {
|
|
this.version = '0.0.1'; // Default version, can be overridden in subclasses
|
|
this.scriptName = path.parse(process.argv[1]).base;
|
|
}
|
|
|
|
getClassName() {
|
|
return this.constructor.name;
|
|
}
|
|
|
|
listMethods() {
|
|
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(this)).filter(
|
|
(prop) => typeof this[prop] === 'function' && prop !== 'constructor' && !prop.startsWith('_')
|
|
);
|
|
return methods;
|
|
}
|
|
|
|
listProperties() {
|
|
const properties = Object.keys(this).filter((prop) => typeof this[prop] !== 'function');
|
|
return properties;
|
|
}
|
|
|
|
discovery() {
|
|
console.log(`My name is '${this.getClassName()}' and I have the version: ${this.version}`);
|
|
console.log(`My methods are:`, this.listMethods());
|
|
console.log(`My properties are:`, this.listProperties());
|
|
}
|
|
|
|
// Method to create a context manager for this instance
|
|
withContext(callback) {
|
|
callback(this);
|
|
}
|
|
|
|
// Higher-order function to decorate other functions and provide logging
|
|
_logDecorator(fn) {
|
|
return function (...args) {
|
|
console.log(`Calling ${fn.name} with arguments:`, args);
|
|
const result = fn.apply(this, args); // Use apply to maintain context
|
|
console.log(`Result of ${fn.name}:`, result);
|
|
return result;
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = This;
|