{"id":22031642,"url":"https://github.com/bsara/get-bound-func","last_synced_at":"2025-03-23T11:41:02.925Z","repository":{"id":79821766,"uuid":"99037067","full_name":"bsara/get-bound-func","owner":"bsara","description":"A tiny library that simplifies function binding and reduces unnecessary function binds.","archived":false,"fork":false,"pushed_at":"2017-12-20T18:17:14.000Z","size":27,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-24T14:58:35.261Z","etag":null,"topics":["bind","binding","function","functions","isc","javascript","js"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/get-bound-func","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bsara.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2017-08-01T19:43:34.000Z","updated_at":"2017-08-08T18:15:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"c20b30b2-ce4a-4cde-9584-25e29a7d0f08","html_url":"https://github.com/bsara/get-bound-func","commit_stats":{"total_commits":17,"total_committers":1,"mean_commits":17.0,"dds":0.0,"last_synced_commit":"b524516977d96fe980812ee03821f2f0649652c5"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsara%2Fget-bound-func","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsara%2Fget-bound-func/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsara%2Fget-bound-func/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsara%2Fget-bound-func/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bsara","download_url":"https://codeload.github.com/bsara/get-bound-func/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245097851,"owners_count":20560316,"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":["bind","binding","function","functions","isc","javascript","js"],"created_at":"2024-11-30T08:19:31.998Z","updated_at":"2025-03-23T11:41:02.904Z","avatar_url":"https://github.com/bsara.png","language":"JavaScript","readme":"# get-bound-func [![NPM Package](https://img.shields.io/npm/v/get-bound-func.svg?style=flat-square)][npm]\n\n\n[![ISC License](https://img.shields.io/badge/license-ISC-blue.svg?style=flat-square)][license]\n\n\nA tiny library that simplifies function binding and reduces unnecessary function binds.\n\nCompatible with AMD, CommonJS, ES6 imports, and global HTML tag inclusion.\n\n\n[Changelog](https://github.com/bsara/get-bound-func/blob/master/CHANGELOG.md)\n\n\n\n\u003cbr/\u003e\n\n\n\n# Install\n\n```bash\n$ npm install --save get-bound-func\n```\n\n\n\n\u003cbr/\u003e\n\n\n\n# Usage\n\n#### [CodePen Example][codepen]\n\n\u003cbr/\u003e\n\n#### ES6 Modules\n\n```js\nimport getBoundFunc, { deleteBoundFuncs } from 'get-bound-func';\n\n\nclass MyClass {\n\n  constructor(msg) {\n    this.val = msg;\n  }\n\n  onClick(e) {\n    console.log(this.val);\n  }\n\n  registerOnClick(element) {\n    element.addEventListener('click', getBoundFunc(this, this.onClick));\n  }\n}\n\n\nconst obj0 = new MyClass(\"Fish fingers and custard\");\nconst obj1 = new MyClass(42);\n\nconst myElement = document.getElementById('myElement');\n\nobj0.registerOnClick(myElement);\nobj1.registerOnClick(myElement);\n\n\nmyElement.click(); // Will output `Fish fingers and custard` and `42` to console\n\n\n// NOTE: It is NOT always required to call `deleteBoundFuncs`.\n// See API docs of `deleteBoundFuncs` for details.\ndeleteBoundFuncs(obj0); // Clears all `obj0` bound functions from memory\ndeleteBoundFuncs(obj1); // Clears all `obj1` bound functions from memory\n```\n\n\u003cbr/\u003e\n\n#### CommonJS\n\n```js\nconst getBoundFunc = rquire('get-bound-func');\n\n\nfunction MyClass(msg) {\n  this.val = msg;\n}\n\nMyClass.prototype.onClick = function(e) {\n  console.log(this.val);\n}\n\nMyClass.prototype.registerOnClick = function(element) {\n  element.addEventListener('click', getBoundFunc(this, this.onClick));\n}\n\n\nvar obj0 = new MyClass(\"Fish fingers and custard\");\nvar obj1 = new MyClass(42);\n\nvar myElement = document.getElementById('myElement');\n\nobj0.registerOnClick(myElement);\nobj1.registerOnClick(myElement);\n\n\nmyElement.click(); // Will output `Fish fingers and custard` and `42` to console\n\n\n// NOTE: It is NOT always required to call `deleteBoundFuncs`.\n// See API docs of `deleteBoundFuncs` for details.\ngetBoundFunc.deleteBoundFuncs(obj0); // Clears all `obj0` bound functions from memory\ngetBoundFunc.deleteBoundFuncs(obj1); // Clears all `obj1` bound functions from memory\n```\n\n\n\n\u003cbr/\u003e\n\n\n\n# API\n\n### getBoundFunc(*context*, *func*)\n\nCreates a new bound function of `func` that is bound to `context` and caches the result.\nIf `getBoundFunc` has already been called for the given `context` and `func`, then the\ncached bound function is returned.\n\n#### Parameters\n\n- **context** (`*`)\n  The context of the returned bound function (I.E. `context` will equal `this` within\n  the scope of the bound function).\n- **func** (`Function`)\n  The function to be bound.\n\n#### Returns\n\nA version of `func` that is bound to `context`.\n\n**Type:** `Function`\n\n\u003cbr/\u003e\n\n### deleteBoundFunc(*context*)\n\nClears from memory all cached bound functions for the given `context`.\n\n\u003e ***NOTE:*** It is not always required to use this function to clear cached bound\n\u003e functions. Behind the scenes, [`WeakMaps`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap)\n\u003e are used to store the cached bound functions, therefore, whenever all other\n\u003e references to the `context` object have been removed, the `context`'s corresponding\n\u003e bound functions in the cache will automatically be cleared for you. However, this\n\u003e function has been made available in the case that you want to manually clear the cache\n\u003e so that memory is freed up sooner, or in instances where the `context` will not be\n\u003e garbage collected.\n\u003e\n\u003e *For more information, refer to the [MDN documentation on `WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap#Why_WeakMap).*\n\n#### Parameters\n\n- **context** (`*`)\n  The context of the bound functions that will be removed from memory.\n\n\n\n\u003cbr/\u003e\n\n\n\n# License\n\nISC License (ISC)\n\nCopyright (c) 2017, Brandon D. Sara (http://bsara.pro/)\n\nPermission to use, copy, modify, and/or distribute this software for any\npurpose with or without fee is hereby granted, provided that the above\ncopyright notice and this permission notice appear in all copies.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\nPERFORMANCE OF THIS SOFTWARE.\n\n\n\n\n[bsara-home]: http://bsara.pro/\n[license]:    https://github.com/bsara/get-bound-func/blob/master/LICENSE \"License\"\n[npm]:        https://www.npmjs.com/package/get-bound-func                \"NPM Package: get-bound-func\"\n[codepen]:    https://codepen.io/bsara/pen/oeLZzo?editors=1011            \"getBoundFunc Example\"\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbsara%2Fget-bound-func","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbsara%2Fget-bound-func","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbsara%2Fget-bound-func/lists"}