Socket
Socket
Sign inDemoInstall

delegates

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

delegates

delegate methods and accessors to another property


Version published
Weekly downloads
15M
increased by1.54%
Maintainers
1
Weekly downloads
 
Created

What is delegates?

The 'delegates' npm package provides a convenient way to create delegate methods and properties on objects, allowing you to forward method calls or property accesses to another object. This is particularly useful for implementing patterns like composition over inheritance, where you want to expose a certain interface on an object without inheriting from another class.

What are delegates's main functionalities?

Method Delegation

This feature allows you to delegate method calls from one object to another. In the provided code, method calls to 'instance.method()' are delegated to 'obj.method()', allowing 'instance' to expose the method of 'obj'.

{
  const delegates = require('delegates');
  const proto = {};
  const obj = { method: function() { return 'method called'; } };
  delegates(proto, 'obj').method('method');
  const instance = Object.create(proto);
  instance.obj = obj;
  console.log(instance.method()); // 'method called'
}

Property Delegation

This feature enables the delegation of property access. In the example, accessing 'instance.prop' will return the value of 'obj.prop'. This is useful for exposing properties of one object on another without direct inheritance.

{
  const delegates = require('delegates');
  const proto = {};
  const obj = { prop: 'value' };
  delegates(proto, 'obj').access('prop');
  const instance = Object.create(proto);
  instance.obj = obj;
  console.log(instance.prop); // 'value'
}

Other packages similar to delegates

Keywords

FAQs

Package last updated on 13 Jan 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