{"id":21188409,"url":"https://github.com/drsensor/optnew","last_synced_at":"2025-03-14T20:25:33.601Z","repository":{"id":57315902,"uuid":"320116241","full_name":"DrSensor/optnew","owner":"DrSensor","description":"Make `new` keyword optional when instantiating ES6 class. Just like in Dart 😉","archived":false,"fork":false,"pushed_at":"2020-12-10T00:37:24.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-10T19:57:17.579Z","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/DrSensor.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}},"created_at":"2020-12-10T00:36:00.000Z","updated_at":"2022-03-23T04:16:14.000Z","dependencies_parsed_at":"2022-09-18T20:52:50.172Z","dependency_job_id":null,"html_url":"https://github.com/DrSensor/optnew","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrSensor%2Foptnew","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrSensor%2Foptnew/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrSensor%2Foptnew/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrSensor%2Foptnew/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DrSensor","download_url":"https://codeload.github.com/DrSensor/optnew/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243640941,"owners_count":20323752,"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-20T18:44:40.807Z","updated_at":"2025-03-14T20:25:33.563Z","avatar_url":"https://github.com/DrSensor.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `optnew`\nMake `new` keyword optional when instantiating es6-class. Just like in Dart 😉\n\n## Usage\n\nGiven this class\n```js\n// all possible ES class features\nclass ToBeInherited {\n  #private = crypto.getRandomValues(new Uint8Array(1))[0]\n\n  public = 2\n\n  get private() { return this.#private }\n\n  static static = 'zap'\n\n  constructor(arg1, arg2) {\n    this.oldstyle = `${this.#private}-${arg1},${arg2}`\n  }\n\n  [Symbol(\"private\")] = crypto.getRandomValues(new Uint32Array(1))[0]\n\n  static [Symbol(\"static\")] = 10\n}\n```\n\n### as decorator\n\n```js\nimport optnew from \"optnew\"\n\n@optnew\nclass D {\n  public\n  static static\n}\n\n@optnew\nclass C extends ToBeInherited {}\n```\n\nresults:\n```js\nconsole.log(C)            // [Function: C] { static: 'zap', [Symbol(\"static\")]: 10 }\nconsole.log(new C(1, 2))  // C { public: 2, oldstyle: '209-1, 2 ', [Symbol(private)]: 1155023846 }\nconsole.log(C(1, 2))      // C { public: 2, oldstyle: '209-1, 2 ', [Symbol(private)]: 1155023846 }\n\nconsole.log(D)      // [Function: D] { static: undefined }\nconsole.log(new D)  // D { public: undefined }\nconsole.log(D())    // D { public: undefined }\n```\n\n### as function\n\n```js\nimport optnew from \"optnew\"\n\nconst A = optnew(class {\n  public\n  static static\n})\n\nconst AI = optnew(class extends ToBeInherited {})\n\nclass C extends ToBeInherited {}\nC = optnew(C)\n```\n\nresults:\n```js\nconsole.log(A)    // [Function: (anonymous)] { static: undefined }\nconsole.log(A)    // (anonymous) { public: undefined }\nconsole.log(A())  // (anonymous) { public: undefined }\n\nconsole.log(AI)            // [Function: (anonymous)] { static: 'zap', [Symbol(\"static\")]: 10 }\nconsole.log(new AI(1, 2))  // (anonymous) { public: 2, oldstyle: '209-1, 2 ', [Symbol(private)]: 1155023846 }\nconsole.log(AI(1, 2))      // (anonymous) { public: 2, oldstyle: '209-1, 2 ', [Symbol(private)]: 1155023846 }\n\nconsole.log(C)            // [Function: C] { static: 'zap', [Symbol(\"static\")]: 10 }\nconsole.log(new C(1, 2))  // C { public: 2, oldstyle: '209-1, 2 ', [Symbol(private)]: 1155023846 }\nconsole.log(C(1, 2))      // C { public: 2, oldstyle: '209-1, 2 ', [Symbol(private)]: 1155023846 }\n\nconsole.log(A.name)   // \"\"\nconsole.log(AI.name)  // \"\"\nconsole.log(C.name)   // \"C\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrsensor%2Foptnew","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrsensor%2Foptnew","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrsensor%2Foptnew/lists"}