
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
atlas-bind-methods
Advanced tools
Binds an instance's prototype methods to itself.
Suppose you have a class which uses instance variables in its methods:
// Animal.js
module.exports = class Animal {
constructor(sound){
this.sound = sound;
}
rawr(){
console.log(this.sound)
}
}
Normally, the following won't work:
const Animal = require("./Animal");
const cat = new Animal("meow!");
const rawr = cat.rawr;
rawr(); // TypeError: Cannot read property 'sound' of undefined
It fails because the reference loses the method's context, but you already knew that! The fix is pretty simple -- just bind the function to the instance:
...
const rawr = cat.rawr.bind(cat)
rawr() // meow!
But, this is kind of annoying when I have to do this for many methods. Enter atlas-bind-methods
...
This package provides a concise function which lets you do the binding in one line:
const Animal = require("./Animal");
const bind = require("atlas-bind-methods");
const cat = bind(new Animal("meow!"));
const rawr = cat.rawr;
rawr() // meow!
Or, if you prefer the side effect, that's fine too:
...
const cat = new Animal("meow!");
bind(cat);
const rawr = cat.rawr;
rawr() // meow!
This doesn't support subclassing, so it would only pre-bind the prototype methods of the direct constructor which is used to instantiate the object.
npm install --save atlas-bind-methods
FAQs
Binds an instance's prototype methods to itself.
The npm package atlas-bind-methods receives a total of 0 weekly downloads. As such, atlas-bind-methods popularity was classified as not popular.
We found that atlas-bind-methods demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.