{"id":22914483,"url":"https://github.com/hernancerm/closures","last_synced_at":"2025-04-01T11:53:00.506Z","repository":{"id":113765900,"uuid":"286320599","full_name":"hernancerm/Closures","owner":"hernancerm","description":"My understanding of closures","archived":false,"fork":false,"pushed_at":"2020-08-09T21:37:12.000Z","size":1,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-14T20:52:58.130Z","etag":null,"topics":["closures","functional-programming"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hernancerm.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2020-08-09T21:36:24.000Z","updated_at":"2023-03-09T00:42:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"25407955-dd96-4836-bef7-90b093fb6ef9","html_url":"https://github.com/hernancerm/Closures","commit_stats":null,"previous_names":["hernancerm/closures"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hernancerm%2FClosures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hernancerm%2FClosures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hernancerm%2FClosures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hernancerm%2FClosures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hernancerm","download_url":"https://codeload.github.com/hernancerm/Closures/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246635950,"owners_count":20809331,"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":["closures","functional-programming"],"created_at":"2024-12-14T05:15:18.147Z","updated_at":"2025-04-01T11:53:00.238Z","avatar_url":"https://github.com/hernancerm.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Closures\n\nA closure is a function — call it «inner function» —, declared within another function — call it «outer function» —, that has access to the outer function's lexical scope *even after* the outer function has returned.\n\nAnother way to put the same idea:\n\nA closure is a nested function which has access to the lexical scope of its outer function *even after* the outer function has returned.\n\nClosure example: [Mouse.js](./Mouse.js)\n\n## Encapsulation through closures\n\nIn OOP, encapsulation is achieved through access modifiers — `public`, `private`, etc. In FP, encapsulation can be achieved through closures, which heavily rely on lexical scope, e.g.\n\n`Mouse.java` — OOP\n\n```java\nclass Mouse {\n  // Encapsulated data\n  private String name;\n\n  public Mouse(String name) {\n    this.name = name;\n  }\n\n  public void sayHello() {\n    System.out.printf(\"Hello! My name is %s\", name);\n  };\n}\n```\n\n`Mouse.js` — FP\n\n```js\nfunction Mouse(state) {\n  // Encapsulated data\n  const name = state.name;\n\n  function sayHello() {\n    console.log(`Hello! My name is ${name}`);\n  }\n\n  return { sayHello };\n}\n```\n\n## Notes about closures\n\n- Lambda functions are a completely independent concept from closures. Closures don't require to be anonymous or named, just to be a function.\n- A closure can only exist in the context of nested functions — functions within functions.\n\n## Useful resources for understanding closures\n\n- Dan Abramov's \u003chttps://whatthefuck.is/closure\u003e\n- Michell Ayala's [Clausuras (Closures) - Javascript](https://youtu.be/KzKLcS65N04)\n- Stack Overflow's [What is lexical scope?](https://stackoverflow.com/questions/1047454/what-is-lexical-scope)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhernancerm%2Fclosures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhernancerm%2Fclosures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhernancerm%2Fclosures/lists"}