Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

component-bind

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

component-bind

function binding utility

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.5M
decreased by-27.22%
Maintainers
1
Weekly downloads
 
Created

What is component-bind?

The component-bind npm package is a utility for binding functions to a specific context, ensuring that the 'this' keyword inside the function refers to the desired object. This is particularly useful in event handling and callback scenarios where the context can be lost.

What are component-bind's main functionalities?

Basic Function Binding

This feature allows you to bind a function to a specific context. In this example, the 'getValue' function is bound to the 'obj' object, ensuring that 'this' inside 'getValue' refers to 'obj'.

const bind = require('component-bind');

const obj = {
  value: 42,
  getValue: function() {
    return this.value;
  }
};

const boundGetValue = bind(obj, obj.getValue);
console.log(boundGetValue()); // 42

Event Handling

This feature is useful for event handling where the context can be lost. By binding 'handleClick' to 'obj', you ensure that 'this' inside 'handleClick' refers to 'obj' when the button is clicked.

const bind = require('component-bind');

const obj = {
  value: 42,
  handleClick: function() {
    console.log(this.value);
  }
};

const button = document.createElement('button');
button.addEventListener('click', bind(obj, obj.handleClick));
document.body.appendChild(button);

Other packages similar to component-bind

Keywords

FAQs

Package last updated on 27 May 2014

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc