{"id":17142918,"url":"https://github.com/js-choi/proposal-function-demethodize","last_synced_at":"2025-06-18T21:33:24.822Z","repository":{"id":45362288,"uuid":"427146232","full_name":"js-choi/proposal-function-demethodize","owner":"js-choi","description":"Draft specification for a standard demethodize function in JavaScript.","archived":false,"fork":false,"pushed_at":"2022-07-09T15:11:13.000Z","size":42,"stargazers_count":11,"open_issues_count":2,"forks_count":1,"subscribers_count":13,"default_branch":"main","last_synced_at":"2025-03-27T02:07:19.195Z","etag":null,"topics":["functional-programming","javascript","object-oriented-programming","tc39"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/js-choi.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-11T21:19:36.000Z","updated_at":"2024-05-28T15:35:36.000Z","dependencies_parsed_at":"2022-09-02T00:22:03.541Z","dependency_job_id":null,"html_url":"https://github.com/js-choi/proposal-function-demethodize","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-choi%2Fproposal-function-demethodize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-choi%2Fproposal-function-demethodize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-choi%2Fproposal-function-demethodize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-choi%2Fproposal-function-demethodize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/js-choi","download_url":"https://codeload.github.com/js-choi/proposal-function-demethodize/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248701650,"owners_count":21148080,"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":["functional-programming","javascript","object-oriented-programming","tc39"],"created_at":"2024-10-14T20:33:11.714Z","updated_at":"2025-04-13T10:41:53.010Z","avatar_url":"https://github.com/js-choi.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Demethodize function for JavaScript\nECMAScript Stage-0 Proposal. J. S. Choi, 2022.\n\n* Formal specification: Not yet\n* Babel plugin: Not yet\n* [Proposal history][HISTORY.md]\n\n[HISTORY.md]: ./HISTORY.md\n\n## Rationale\nThe dynamic `this` binding is a fundamental part of JavaScript design and\npractice today. Many APIs use the `this` binding functionally as another\n“argument” to their functions. Because of this, developers frequently need to\nchange the `this` binding. However, dynamic prototype-based dispatch may often\nbe undesirable, and using a `.call.bind()` method is frequent in many\ncodebases.\n\nWe therefore propose exploring the addition of an API to the JavaScript\nlanguage that would convert `this`-based methods into ordinary functions that\ndo not use `this`.\n\nIf this proposal is approved for Stage 1, then we would explore various\ndirections for the API’s design. We would also assemble as many real-world use\ncases as possible and shape our design to fulfill them.\n\nIn addition, if we would explore cross-cutting concerns and overlap with the\nproposed [call-this syntax][] and other [dataflow proposals][].\n\n[dataflow proposals]: https://jschoi.org/22/es-dataflow/\n\n## Description\nThe `Function.prototype.demethodize` method would create a new function that\ncalls the original function, supplying its first argument as the original\nfunction’s `this` receiver, and supplying the rest of its arguments as the\noriginal function’s ordinary arguments.\n\nThis is useful for converting `this`-based functions into non-`this`-based\nfunctions.\n\n```js\nfn.demethodize();\n\nconst $slice = Array.prototype.slice.demethodize();\n$slice([ 0, 1, 2 ], 1); // [ 1, 2 ].\n```\n\nThis is not a substitute for a [call-this syntax][], which allows developers to\nchange the receiver of functions without creating a wrapper function.\n\n[call-this syntax]: https://github.com/tc39/proposal-call-this\n\n`fn.demethodize()` is equivalent to\\\n`Function.prototype.call.bind(fn)` and to\\\n`Function.prototype.bind.bind(Function.prototype.call)(fn)`.\n\nTherefore, `fn.demethodize()(thisArg, ...restArgs)` is equivalent to\n`fn.call(thisArg, ...restArgs)`.\n\nThe following real-world example originally used [call-bind][] or a manually\ncreated similar function.\n\n```js\n// From chrome-devtools-frontend@1.0.934332\n// node_modules/array-includes/test/implementation.js.\nrunTests(implementation.demethodize(), t);\n\n// From string.prototype.trimstart@1.0.4/index.js:\nvar bound = getPolyfill().demethodize();\n\n// From andreasgal/dom.js (84b7ab6) src/snapshot.js.\nconst /* … */\n  join = A.join || Array.prototype.join.demethodize(),\n  map = A.map || Array.prototype.map.demethodize(),\n  push = A.push || Array.prototype.push.demethodize(),\n  /* … */;\n```\n\nPrecedents include:\n* [call-bind][]: `callBind`\n\n[call-bind]: https://www.npmjs.com/package/call-bind\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjs-choi%2Fproposal-function-demethodize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjs-choi%2Fproposal-function-demethodize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjs-choi%2Fproposal-function-demethodize/lists"}