{"id":15893401,"url":"https://github.com/multimeric/customclass","last_synced_at":"2025-04-02T17:42:53.554Z","repository":{"id":57211125,"uuid":"123291574","full_name":"multimeric/CustomClass","owner":"multimeric","description":"Modify the internal behaviour of your JavaScript classes","archived":false,"fork":false,"pushed_at":"2018-03-22T14:29:50.000Z","size":31,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-21T05:23:30.415Z","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":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/multimeric.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":"2018-02-28T13:51:39.000Z","updated_at":"2018-03-22T18:19:07.000Z","dependencies_parsed_at":"2022-09-01T04:22:25.429Z","dependency_job_id":null,"html_url":"https://github.com/multimeric/CustomClass","commit_stats":null,"previous_names":["tmiguelt/customizableclassjs"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multimeric%2FCustomClass","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multimeric%2FCustomClass/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multimeric%2FCustomClass/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multimeric%2FCustomClass/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/multimeric","download_url":"https://codeload.github.com/multimeric/CustomClass/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246863815,"owners_count":20846317,"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-06T08:10:23.463Z","updated_at":"2025-04-02T17:42:53.535Z","avatar_url":"https://github.com/multimeric.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CustomClass\n* [Introduction](#introduction)\n* [Installation](#installation)\n* [Usage](#usage)\n  + [Common Arguments](#common-arguments)\n  + [List of Methods](#list-of-methods)\n    - [`__apply__(target, thisArg, argumentsList, getDefault)`](#__apply__target-thisarg-argumentslist-getdefault)\n    - [`__construct__(target, argumentsList, newTarget, getDefault)`](#__construct__target-argumentslist-newtarget-getdefault)\n    - [`__defineProperty__(target, property, descriptor, getDefault)`](#__defineproperty__target-property-descriptor-getdefault)\n    - [`__deleteProperty__(target, property, getDefault)`](#__deleteproperty__target-property-getdefault)\n    - [`__get__(target, property, receiver, getDefault)`](#__get__target-property-receiver-getdefault)\n    - [`__getOwnPropertyDescriptor__(target, prop, getDefault)`](#__getownpropertydescriptor__target-prop-getdefault)\n    - [`__getPrototypeOf__(target, getDefault)`](#__getprototypeof__target-getdefault)\n    - [`__has__(target, property, getDefault)`](#__has__target-property-getdefault)\n    - [`__isExtensible__(target, getDefault)`](#__isextensible__target-getdefault)\n    - [`__ownKeys__(target, getDefault)`](#__ownkeys__target-getdefault)\n    - [`__preventExtensions__(target, getDefault)`](#__preventextensions__target-getdefault)\n    - [`__set__(target, property, value, receiver, getDefault)`](#__set__target-property-value-receiver-getdefault)\n    - [`__setPrototypeOf__(target, prototype)`](#__setprototypeof__target-prototype)\n* [Example: Making a defaultdict](#example-making-a-defaultdict)\n\n## Introduction\n\n`CustomClass` allows you to customize the internal methods of your JavaScript classes, in the same way that you might\nin other languages like Python or Ruby.\n\nFor example, you might want an object that lets you call it like a function:\n\n```javascript\nclass MyClass extends CustomClass {\n    __apply__(){\n        return \"I'm a function!\";\n    }\n}\n\nconst mc = new MyClass();\nconsole.log(mc());\n```\n```\nI'm a function!\n```\n\nOr maybe you want an object that has a default value for any key you try to access:\n```javascript\nclass MyClass extends CustomClass {\n    __get__(target, property, receiver, getDefault) {\n        if (prop in target) {\n            return getDefault();\n        }\n        else {\n            return \"DEFAULT VALUE\";\n        }\n    }\n}\n\nconst mc = new MyClass();\nconsole.log(mc.foo);\nconsole.log(mc.bar);\n```\n\n```\nDEFAULT VALUE\nDEFAULT VALUE\n```\n\n## Installation\nRun:\n\n```bash\nnpm install custom-class --save\n```\n\nThen import the class:\n\n```javascript\nconst CustomClass = require('custom-class');\n```\n\n## Usage\nSimply inherit from the `CustomClass`, and implement any of the following double-underscore methods. In general, the\nsignature of these methods matches the corresponding methods on the\n[JavaScript `Proxy` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler#Methods),\nbut with an extra argument added on to the end, a function that will return the default value for this method.\n\n### Common Arguments\n\n* `target`: This object, but without any internal method intercepting. Use this to get information out of your object\nwithout fear of causing an infinite loop\n* `getDefault`: A function that, if called, will perform the default behaviour and return the default value for this\nmethod. For example if you override `__get__`, `getDefault()` will return the true value of the field the user is trying\nto access.\n\n### List of Methods\n(Content based on [Proxy handler](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler) by\n[Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler$history),\n licensed under [CC-BY-SA 2.5](http://creativecommons.org/licenses/by-sa/2.5/))\n\n#### `__apply__(target, thisArg, argumentsList, getDefault)`\nInvoked when an instance of this class is called as a function\n\ne.g. `myInstance()`\n\n* `thisArg`: The `this` argument for the call\n* `argumentsList`: The list of arguments for the call\n\n#### `__construct__(target, argumentsList, newTarget, getDefault)`\nInvoked when an instance of this class is used as a constructor\n\ne.g. `new myInstance()`\n* `argumentsList`: The list of arguments for the constructor\n* `newTarget`: The object that is being constructed\n\n#### `__defineProperty__(target, property, descriptor, getDefault)`\nInvoked when an instance of this class has `Object.defineProperty()` called on it\n* `property`: The name of the property whose description is to be retrieved\n* `descriptor`: The descriptor for the property being defined or modified\n\n#### `__deleteProperty__(target, property, getDefault)`\nInvoked when an instance of this class has one of its fields deleted\n\ne.g. `delete myInstance.foo`\n* `property`: The name of the property to delete\n\n#### `__get__(target, property, receiver, getDefault)`\nInvoked when an instance of this class has one of its fields accessed\n\ne.g. `myInstance.foo`\n* `property`: The name of the property to get\n* `receiver`: Either the proxy or an object that inherits from the proxy\n\n#### `__getOwnPropertyDescriptor__(target, prop, getDefault)`\n\nInvoked when an instance of this class has `Object.getOwnPropertyDescriptor()` called on it\n* `prop`: The name of the property whose description should be retrieved\n\n#### `__getPrototypeOf__(target, getDefault)`\nInvoked when an instance of this class has its prototype checked,\n\ne.g. `Object.getPrototypeOf(myInstance)`, or `myInstance instanceof SomeClass`\n\n#### `__has__(target, property, getDefault)`\nInvoked when an instance of this class has the `in` operator applied to it\n\ne.g. `\"foo\" in myInstance`\n* `property`: The name of the property to check for existence.\n#### `__isExtensible__(target, getDefault)`\nInvoked when an instance of this class has `Object.isExtensible()` called on it\n\n#### `__ownKeys__(target, getDefault)`\nInvoked when an instance of this class has `Object.getOwnPropertyNames()` called on it\n#### `__preventExtensions__(target, getDefault)`\nInvoked when an instance of this class has `Object.preventExtensions()`  called on it.\n\n#### `__set__(target, property, value, receiver, getDefault)`\n\nInvoked when an instance of this class has one of its fields set\n\ne.g. `myInstance.foo = \"bar\"`\n* `property`: The name of the property to set\n* `value`: The new value of the property to set\n* `The object to which the assignment was originally directed`\n\n#### `__setPrototypeOf__(target, prototype)`\nInvoked when an instance of this class has its prototype set\n* `prototype`: The object's new prototype or `null` using\n`Object.setPrototypeOf()`\n\n## Example: Making a defaultdict\n\nIn this example, you want to make a JavaScript equivalent of Python's `defaultdict`: a dictionary that has a default\nvalue for all keys you haven't set yourself:\n\n```javascript\nclass DefaultDict extends CustomClass {\n    constructor(defaultConstructor) {\n        super();\n        this.defaultConstructor = defaultConstructor;\n    }\n\n    __get__(target, prop, receiver, getDefault) {\n        if (prop in target) {\n            // If we already have a value for this, return it\n            return getDefault();\n        }\n        else {\n            // If we don't, generate a default value using our default constructor, and save it onto the object\n            const generated = new this.defaultConstructor();\n            target[prop] = generated;\n            return generated;\n        }\n    }\n}\n\nconst dd = new DefaultDict(Array);\n\nassert.deepEqual(dd.foo, []);\n\ndd.bar.push('a');\nassert.deepEqual(dd.bar, ['a'])\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmultimeric%2Fcustomclass","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmultimeric%2Fcustomclass","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmultimeric%2Fcustomclass/lists"}