ITADN

Decorator Customization with Centralized Management Approach

#563Closedalpgul 创建于 2025-08-18
A
alpgulcommented
## Problem: Limitations of Current Decorator Management In a project with existing Decorator usage, if we want to customize all Decorators, we would need to recreate or redefine all decorator implementations. However, if we had a global Decorator definition, we could simply override this definition and avoid this complex process. ## Solution: Global Decorator Prototyping ### Basic Approach ```typescript // Override Central Decorator const originalDecorator = globalThis.Decorator; globalThis.Decorator = class { constructor(fn) { const newFn = function() { // Custom implementation ... } return originalDecorator(newFn); } } // Usage Example const logged = new Decorator(function (value, { kind, name }) { if (kind === "method") { return function (...args) { console.log(`starting ${name} with arguments ${args.join(", ")}`); const ret = value.call(this, ...args); console.log(`ending ${name}`); return ret; }; } }); class C { @logged m(arg) {} } ``` ## Advantages - Manage all decorators from a single point - Easily modify global behaviors - Reduces code repetition - Provides a flexible architecture - Simplifies decorator management - Increases readability
关闭于 2025-08-19 3 条评论