Function bindMethodToSelfByName

binds a class-prototype-method (by name method_name) to the passed object self (which should be an instance of the class), and returns that bound method.

type ID = number
const graph_edges = new Map<ID, Set<ID>>()
const set_graph_edge = bindMethodToSelfByName(graph_edges, "set") // automatic type inference will correctly assign it the type: `(key: number, value: Set<number>) => Map<number, Set<number>>`
const edges: [ID, ID[]][] = [[1, [1,2,3]], [2, [3,5]], [3, [4, 7]], [4, [4,5]], [5, [7]]]
for (const [id, adjacent_ids] of edges) { set_graph_edge(id, new Set(adjacent_ids)) }

example with assigned default arguments

import { assertEquals } from "jsr:@std/assert"

const queue = [1, 2, 3, 7, 7, 7, 9, 9, 9]
const release_from_queue = bindMethodToSelfByName(queue, "splice", 0) // automatic type inference will correctly assign it the type: `(deleteCount: number, ...items: number[]) => number[]`
const test_arr: number[][] = []
while (queue.length > 0) { test_arr.push(release_from_queue(3)) }
assertEquals(test_arr, [
[1, 2, 3],
[7, 7, 7],
[9, 9, 9],
])
  • Type Parameters

    • S extends Record<M, BindableFunction<S, A, any[], any>>
    • M extends PropertyKey
    • A extends unknown[]
    • R extends any

    Parameters

    • self: S

      the object to bind the method method_name to

    • method_name: M

      the name of the prototype-method to bind

    • Rest...args: A

      partial tuple of the first few arguments that should be passed in by default

    Returns ((...args: S[M] extends BindableFunction<S, A, B, R>
        ? B
        : never) => R)

    a version of the function method_name that is now bound to the object self, with the default first few partial arguments args