https://github.com/maximilianmairinger/attachtoprototype
Attatch functions as non enumerable properties to the prototype of any object.
https://github.com/maximilianmairinger/attachtoprototype
attach enumerable prototype
Last synced: 3 months ago
JSON representation
Attatch functions as non enumerable properties to the prototype of any object.
- Host: GitHub
- URL: https://github.com/maximilianmairinger/attachtoprototype
- Owner: maximilianMairinger
- Created: 2020-02-03T21:10:47.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-07-22T10:57:39.000Z (almost 2 years ago)
- Last Synced: 2025-02-24T05:16:19.057Z (4 months ago)
- Topics: attach, enumerable, prototype
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/attatch-to-prototype
- Size: 35.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Attach to prototype
Attach functions as non enumerable properties to the prototype of any object.
> Please not that Attach to prototype is currently under development and not yet suited for production
## Example
### Generic
```ts
import { constructAttachToPrototype } from "attatch-to-prototype"const attachToArray = constructAttachToPrototype(Array.prototype/*, options*/)
attachToArray("removeByValue", function(value) {
const index = this.indexOf(value)
if (index === -1) return
this.splice(index, 1)
})const ar = ["a", "b", "c"]
ar.removeByValue("b")
console.log(ar) // ["a", "c"]
```
### Getter / Setter#### Attach
```ts
attachToArray("last", {
get() {
return this[this.length-1]
}
set(to) {
return this[this.length-1] = to
}
})let ar = [1, 2, 3]
ar.last // return 3
ar.last = 0 // return 0console.log(ar) // [1, 2, 0]
```#### Apply
Just a different syntax to access getter / setter. Use Attach / apply depending on your coding conventions.
```ts
import { constructApplyToPrototype } from "attatch-to-prototype"const attachToArray = constructApplyToPrototype(Array.prototype/*, options*/)
attachToArray("last", {
get() {
return this[this.length-1]
}
set(to) {
return this[this.length-1] = to
}
})let ar = [1, 2, 3]
ar.last() // return 3
ar.last(0) // return 0console.log(ar) // [1, 2, 0]
```The [generic](#Generic) functionality is also available on apply
## Contribute
All feedback is appreciated. Create a pull request or write an issue.