{"id":19574683,"url":"https://github.com/stringparser/createclass","last_synced_at":"2026-05-15T00:08:12.317Z","repository":{"id":82473670,"uuid":"65937298","full_name":"stringparser/createClass","owner":"stringparser","description":"React-like createClass","archived":false,"fork":false,"pushed_at":"2016-09-02T20:43:45.000Z","size":25,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-09T21:46:43.259Z","etag":null,"topics":[],"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/stringparser.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":"2016-08-17T19:50:15.000Z","updated_at":"2019-01-29T15:53:58.000Z","dependencies_parsed_at":"2023-06-04T17:15:40.189Z","dependency_job_id":null,"html_url":"https://github.com/stringparser/createClass","commit_stats":{"total_commits":38,"total_committers":1,"mean_commits":38.0,"dds":0.0,"last_synced_commit":"d87219d996586b378e042f22ad960cb5c57a81a8"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stringparser%2FcreateClass","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stringparser%2FcreateClass/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stringparser%2FcreateClass/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stringparser%2FcreateClass/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stringparser","download_url":"https://codeload.github.com/stringparser/createClass/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240840848,"owners_count":19866280,"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-11T06:43:24.521Z","updated_at":"2026-05-15T00:08:11.549Z","avatar_url":"https://github.com/stringparser.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# createClass [![NPM version][b-version]][npm] [![downloads][badge-downloads]][npm]\n\n[![build][b-build]][travis]\n\n[docs](#docs) -\n[install](#install)\n\nJust sugar.\n\n## usage\n\nContrived example time! :D\n\n```js\nvar createClass = require('createClass');\n\nvar Logger = createClass({\n  create: function Logger (props) {\n    console.log(this.constructor.name, 'created with props', props);\n\n    Object.keys(this.constructor.prototype).forEach(function (method) {\n      var self = this;\n      self[method] = function (/* arguments */) {\n        console.log(self.constructor.name, 'is going to', method);\n        self.constructor.prototype[method].apply(self, arguments);\n      };\n    }, this);\n  }\n});\n\nvar Animal = createClass(Logger, {\n  create: function Animal (props) {\n    Animal.super_.call(this, props);\n    this.props = props;\n  }\n});\n\nvar FelineMixin = {\n  talk: function () {\n    console.log('meoww (%s)', this.props.thinking);\n  }\n};\n\nvar Cat = Animal.createClass({\n  mixins: [FelineMixin],\n  create: function Cat (props) {\n    Cat.super_.call(this, props);\n  }\n});\n\nvar cat = new Cat({ thinking: 'hungry' });\n\nsetInterval(function () {\n  cat.talk();\n}, 100);\n```\n\n## docs\n\nThe `module.exports` a function\n\n```js\nvar createClass = require('createClass');\n```\n\nWhen assigning `createClass` to a function makes it the super class.\n\n```js\nfunction Constructor () {}\nConstructor.createClass = createClass;\n\nvar Child = Constructor.createClass();\n// behold! Child will inherit from `Constructor` now\n```\n\n## createClass\n\nCan take 2 arguments\n\n```js\nfunction createClass ([Function Super, Object spec])\n```\n\n_arguments_\n- `Super`, type `function`, to use as super class\n- `spec`, type `object`, to be added to the constructor prototype. Can be either passed as 1st or 2nd argument to the function\n\n_returns_ a new class with the given `spec` in its prototype and two static methods\n\n- `NewClass.create`: creates a new instance (up to 3 arguments)\n- `NewClass.createClass`: same as `createClass` but using `NewClass` as super\n\nexample:\n\n```js\nvar Animal = createClass({\n  talk: function () {\n    console.log('?');\n  }\n});\n\nvar Cat = createClass(Animal, {\n  talk: function () {\n    console.log('meoww');\n  }\n});\n\nvar cat = Cat.create();\n\ncat.talk();\n// =\u003e meoww\n```\n\n`spec` has some special properties\n\n### spec.create\n\nShould be a `Function`.\n\nIf given, it will be used the constructor function for the returned class.\n\nThe module is using [`inherits`](https://github.com/isaacs/inherits) to do inheritance which means: `super_` is set as a static property.\n\n```js\nvar Animal = createClass({\n  create: function Animal (props) {\n    this.props = props;\n  }\n});\n```\n\nand since `Super` can be passed as first argument\n\n```js\nvar Cat = createClass(Animal, {\n  create: function Cat (/* arguments */) {\n    Cat.super_.apply(this, arguments);\n  }\n});\n\nvar cat = Cat.create({ thinking: 'hungry' });\n\nconsole.log(cat.props);\n// =\u003e { thinking: 'hungry' }\n```\n\n### spec.mixins\n\nSame as `merge`/`extend`/`Object.assign` with the resulting class prototype. The element at the tail of the array is the first to be used and from there back to the beginning of the array.\n\nEach element of the `Array` can be `object` or `function`.\n\nWhen an element is ...\n\n- `object`, its properties are directly assigned\n- `function`, its prototype (enumerable) properties are assigned\n\nIn both cases there is no method override. If was defined on already it will not be overridden.\n\n```js\nfunction Animal () {}\nAnimal.prototype.talk = function () {\n  console.log('Yo!');\n};\n\nfunction Mammal () {}\nMammal.prototype.hasFur = function () {\n  return true;\n};\n\nvar AnimalMixin = {\n  poop: function () {\n    console.log('Pooping now. Wait for it...');\n    setTimeout(function () {\n      console.log('.');\n    }, Math.random()*1000);\n  }\n};\n\nvar Bear = createClass(Animal, {\n  mixins: [AnimalMixin, Mammal]\n  poop: function () {\n    console.log('Bear pooping now!');\n    console.log('.');\n    console.log('Bear pooped!');\n  },\n  talk: function () {\n    console.log('bearrrrrrr!');\n  }\n});\n\nvar Cat = createClass(Animal, {\n  mixins: [AnimalMixin, Mammal]\n  talk: function () {\n    console.log('meowww');\n  }\n});\n```\n\nMixins are good looking because you can put them together with just objects, and one may think there is less of the \"[you wanted a bannana but you got a gorilla holding a banana](http://www.johndcook.com/blog/2011/07/19/you-wanted-banana/)\" problem,  but there are still issues with them see: [mixins are considered harmful](https://facebook.github.io/react/blog/2016/07/13/mixins-considered-harmful.html#why-mixins-are-broken).\n\nSo we should be careful grasshoppers.\n\n### spec.statics\n\nObject of static properties to add to the new class.\n\n```js\nvar Animal = createClass({\n  statics: {\n    isMammal: function (animal) {\n      if (animal \u0026\u0026 typeof animal !== 'function') {\n        return animal.props \u0026\u0026 animal.props.neocortex;\n      }\n    }\n  }\n});\n```\n\n# install\n\nWith [npm](https://npmjs.com)\n\n```sh\nnpm install --save-dev createClass\n```\n\n### license\n\nThe MIT License (MIT)\n\nCopyright (c) 2016-present Javier Carrillo\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n\u003c!-- links --\u003e\n[npm]: https://npmjs.com/createClass\n[travis]: https://travis-ci.org/stringparser/createClass/builds\n\n[b-build]: https://travis-ci.org/stringparser/createClass.svg?branch=master\n[b-version]: http://img.shields.io/npm/v/createClass.svg?style=flat-square\n[badge-downloads]: http://img.shields.io/npm/dm/createClass.svg?style=flat-square\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstringparser%2Fcreateclass","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstringparser%2Fcreateclass","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstringparser%2Fcreateclass/lists"}