{"id":15705924,"url":"https://github.com/benjamn/cls","last_synced_at":"2025-07-08T15:04:07.427Z","repository":{"id":8968570,"uuid":"10710437","full_name":"benjamn/cls","owner":"benjamn","description":"Class factory featuring inheritance of static properties, static constructors, lazy population of prototypes, and this._super","archived":false,"fork":false,"pushed_at":"2017-03-18T16:27:06.000Z","size":11,"stargazers_count":3,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-12T22:18:53.447Z","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/benjamn.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":"2013-06-15T18:45:13.000Z","updated_at":"2019-03-18T04:32:25.000Z","dependencies_parsed_at":"2022-09-06T03:12:14.678Z","dependency_job_id":null,"html_url":"https://github.com/benjamn/cls","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/benjamn/cls","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benjamn%2Fcls","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benjamn%2Fcls/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benjamn%2Fcls/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benjamn%2Fcls/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benjamn","download_url":"https://codeload.github.com/benjamn/cls/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benjamn%2Fcls/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264292907,"owners_count":23586059,"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-10-03T20:20:41.329Z","updated_at":"2025-07-08T15:04:07.409Z","avatar_url":"https://github.com/benjamn.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Introduction\n---\n\nStandardized `class` syntax\n[is coming](http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes)\nin ECMAScript 6, supposedly, but until then I need a class factory that fills the gap.\n\nFor me that means (in no particular order):\n\n* prototypal inheritance under the hood\n* access to overridden properties (`super`)\n* inheritance of `static` properties\n* `static` constructors\n* close correspondence to ES6 syntax\n* ES5/browser compatibility\n* only-pay-for-what-you-use performance\n* excellent test coverage: [![Build Status](https://travis-ci.org/benjamn/cls.png?branch=master)](https://travis-ci.org/benjamn/cls)\n\nI have no delusions of persuading the world to use this tool. Just try\n`npm search inheritance` some time to see how many other people have come\nup with solutions that work for them.\n\nIf you have a special interest in the tired me-too sport of\npure-JavaScript class factory implementations, you might find this one\ninteresting for its solutions to each of the requirements listed above,\nparticularly the lazy (just-in-time) population of prototype properties.\n\nNote that I did not mention privacy enforcement as a requirement. If you\nneed a mechanism like the `private` keyword in other languages, I have a\nseparate [project](https://npmjs.org/package/private) that works\nseamlessly alongside this one.\n\nInstallation\n---\nFrom NPM:\n\n    npm install cls\n\nFrom GitHub:\n\n    cd path/to/node_modules\n    git clone git://github.com/benjamn/cls.git\n    cd cls\n    npm install .\n\nUsage\n---\n\nOne example will have to suffice for now:\n```js\nvar cls = require(\"cls\");\n\nvar BaseClass = cls.extend({\n    init: function(a, b) {\n        this.sum = a + b;\n    },\n\n    getSum: function() {\n        return this.sum;\n    },\n\n    statics: {\n        name: \"BaseClass\",\n\n        init: function(cls) {\n            cls.zero = new cls(0, 0);\n        }\n    }\n});\n\nvar SubClass = BaseClass.extend({\n    init: function(arg) {\n        this._super(arg, arg);\n        this.sum += 1;\n    },\n\n    statics: {\n        name: \"SubClass\"\n    }\n});\n\nassert(BaseClass.name === \"BaseClass\");\nassert(SubClass.name === \"SubClass\");\n\nassert(new BaseClass(2).getSum() === 4);\nassert(new SubClass(2).getSum() === 5);\n\nassert(SubClass.zero !== BaseClass.zero);\nassert(SubClass.zero instanceof SubClass);\nassert(SubClass.zero.getSum() === 1);\n```\nFor more complex examples, see `test/run.js`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenjamn%2Fcls","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenjamn%2Fcls","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenjamn%2Fcls/lists"}