Socket
Socket
Sign inDemoInstall

attatch-to-prototype

Package Overview
Dependencies
69
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    attatch-to-prototype

Attach functions as non enumerable properties to the prototype of any object.


Version published
Maintainers
1
Created

Readme

Source

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

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
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 0

console.log(ar) // [1, 2, 0]
Apply

Just a different syntax to access getter / setter. Use Attach / apply depending on your coding conventions.

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 0

console.log(ar) // [1, 2, 0]

The generic functionality is also available on apply

Contribute

All feedback is appreciated. Create a pull request or write an issue.

Keywords

FAQs

Last updated on 22 Jul 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc