@@ -260,6 +260,46 @@ export function bind<Args extends readonly unknown[], T, U>(fn: (this: T, ...arg
260
260
return ( ...args : Args ) => fn . call ( thisArg , ...args ) ;
261
261
}
262
262
263
+ /**
264
+ * Creates a proxy for an object that ensures all its function properties are bound to the object itself.
265
+ * This can be particularly useful when you want to pass an object's method as a callback without losing its context.
266
+ *
267
+ * @param {T } thisArg - The object for which to bind all its function properties.
268
+ * @returns {T } A proxy of the same type as `thisArg` where every function property, when accessed,
269
+ * is automatically bound to `thisArg`. Non-function properties are accessed as usual.
270
+ *
271
+ * @example
272
+ * ```typescript
273
+ * class Example {
274
+ * constructor(public name: string) {}
275
+ * greet() {
276
+ * console.log(`Hello, ${this.name}!`);
277
+ * }
278
+ * }
279
+ *
280
+ * const example = new Example('World');
281
+ * const boundExample = bindSelf(example);
282
+ * const greet = boundExample.greet;
283
+ * greet(); // Logs: "Hello, World!" - `this` context is preserved due to binding.
284
+ * ```
285
+ *
286
+ * Note: This function uses JavaScript's Proxy and Reflect APIs to intercept property accesses
287
+ * and bind functions dynamically. It works at runtime and relies on TypeScript for type safety only.
288
+ * Be cautious that the use of `as T` for the return type is a type assertion that assumes the proxy
289
+ * maintains the same type interface as `thisArg`, which TypeScript cannot verify for dynamic property access.
290
+ */
291
+ export function bindSelf < T extends object > ( thisArg : T ) : T {
292
+ return new Proxy ( thisArg , {
293
+ get ( target , prop , receiver ) {
294
+ const value = Reflect . get ( target , prop , receiver ) ;
295
+ if ( typeof value === 'function' ) {
296
+ return value . bind ( thisArg ) ;
297
+ }
298
+ return value ;
299
+ }
300
+ } ) as T ;
301
+ }
302
+
263
303
/**
264
304
* Memoizes a function.
265
305
* @param fn function to memoize
0 commit comments