{"id":20513933,"url":"https://github.com/webreflection/custom-function","last_synced_at":"2025-04-09T16:18:28.856Z","repository":{"id":65810602,"uuid":"600432696","full_name":"WebReflection/custom-function","owner":"WebReflection","description":"Literally the only sane way, if not the fastest one, to extend the Function class witohut evaluation.","archived":false,"fork":false,"pushed_at":"2024-10-23T12:23:28.000Z","size":105,"stargazers_count":38,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-09T16:18:21.351Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WebReflection.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-11T13:33:07.000Z","updated_at":"2025-01-06T20:24:08.000Z","dependencies_parsed_at":"2025-03-16T16:01:12.351Z","dependency_job_id":null,"html_url":"https://github.com/WebReflection/custom-function","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"d0c56c2795d27372fdc91cb9d791f60614fff0ff"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fcustom-function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fcustom-function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fcustom-function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fcustom-function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WebReflection","download_url":"https://codeload.github.com/WebReflection/custom-function/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248065281,"owners_count":21041872,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T21:13:56.227Z","updated_at":"2025-04-09T16:18:28.839Z","avatar_url":"https://github.com/WebReflection.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# custom-function\n\n[![Coverage Status](https://coveralls.io/repos/github/WebReflection/custom-function/badge.svg?branch=main)](https://coveralls.io/github/WebReflection/custom-function?branch=main) [![build status](https://github.com/WebReflection/custom-function/actions/workflows/node.js.yml/badge.svg)](https://github.com/WebReflection/custom-function/actions)\n\n\u003csup\u003e**Social Media Photo by [Aaron Huber](https://unsplash.com/@aahubs) on [Unsplash](https://unsplash.com/)**\u003c/sup\u003e\n\nLiterally the only sane way, if not the fastest one, to extend the `Function` class **without evaluation**.\n\n```js\n// const CustomFunction = require('custom-function');\nimport CustomFunction from 'custom-function';\n\nclass MyFunction extends CustomFunction {\n  invoke(...args) {\n    return this(...args);\n  }\n  toString() {\n    return '[native code]';\n  }\n}\n\nconst cf = new MyFunction((a, b) =\u003e a + b);\ncf(1, 2);         // 3\ncf.invoke(1, 2);  // 3\ncf.toString();    // \"[native code]\"\n```\n\n### Why using a Class?\n\n`Object.assign(fn, {...extras})` can augment `fn` but it cannot have accessors, private fields, `super.method(...args)` calls, and so on, while with this module, everything possible with classes is possible with functions too.\n\n`Object.defineProperties` can have accessors and not all enumerable and writable fields, but it still cannot set private fields or allow `super.method(...args)` calls.\n\nOn top of these limitations, adding *O(n)* features requires more effort than swapping once the prototypal chain, where the original `Function.prototype` root of the chain will be preserved regardless.\n\nAs summary: every solution to date that is not based on native `class extends` feature is somehow limited, likely slower, surely inferior in terms of DX.\n\n### Why only Function?\n\nWhile writing this I had `Function` as one problematic constructor that cannot be extended due inevitable code evaluation involved while invoking `super()` but it's true that this approach/pattern can be used for any constructor that could be problematic if invoked right away, which is why there is a `custom-function/factory` export too so that anything becomes possible, example:\n\n```js\n// const custom = require('custom-function/factory');\nimport custom from 'custom-function/factory';\n\n// reproduce exactly what this module provides:\nconst CustomFunction = custom(Function);\nclass MyFunction extends CustomFunction {}\n\n// go wild with any illegal constructor too\nconst Div = custom(HTMLDivElement);\nclass MyDiv extends Div {\n  constructor(...childNodes) {\n    super(document.createElement('div'));\n    this.append(...childNodes);\n  }\n}\n\ndocument.body.appendChild(\n  new MyDiv(\n    new MyDiv('A'),\n    new MyDiv('B', 'C')\n  )\n);\n```\n\n### Exports\n\n  * **custom-function** to extend functions without invoking `Function`\n  * **custom-function/factory** to extend any class without invoking the extended class constructor\n  * **custom-function/closure** to extend functions in a *Closure Compiler* friendly way\n  * **custom-function/closure-factory** to extend any class in a *Closure Compiler* friendly way\n\n### Performance\n\nThe [benchmark](./test/benchmark.js) test properties added to a callback using this module VS using a predefined *descriptors* object via `Object.defineProperties` to compare the most possible perf-tuned define properties approach against this module pattern.\n\nUse `npm run bench` to test yourself locally, after cloning this repo.\n\n```\ncold run\nCustomFunction\n  creation: 20.884ms\n  metohd: 3.421ms\nObject.defineProperties\n  creation: 76.917ms\n  metohd: 4.838ms\n\nhot run\nCustomFunction\n  creation: 7.597ms\n  metohd: 0.667ms\nObject.defineProperties\n  creation: 94.064ms\n  metohd: 1.359ms\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fcustom-function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebreflection%2Fcustom-function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fcustom-function/lists"}