
Product
Go Support Is Now Generally Available
Socket's Go support is now generally available, bringing automatic scanning and deep code analysis to all users with Go projects.
bindall-standalone
Advanced tools
Standalone, improved version of underscore's `_.bindAll()` function for IE9+ browsers.
Allow to permanently mutate an object's method so it context this
will always be bound to this object.
Allow to avoid the non-unbindable listeners registered via emitter.on(foo, this.bar.bind(this));
.
npm install bindall-standalone --save
Then just var bindAll = require('bindall-standalone')
. Works with require() e.g. node.js, browserify or component(1).
bindAll(object, *methods);
Mutates all methods from object
, passed as a list of strings (such as 'foo', 'bar'
) so they always will be called with the context bound to the object
.
bindAll(object);
Bind ALL methods available on the object.
var bindAll = require('bindall-standalone');
var object = {
foo: 10,
bar: function() {
return this.foo;
}
};
object.bar(); // 10
var func = object.bar;
func(); // undefined
bindAll(object, 'bar');
var func = object.bar;
func(); // 10
var bindAll = require('bindall-standalone');
var Foo = function() {
bindAll(this, 'onReady');
// mediator.on('ready', this.onReady.bind(this)); // Never going to be unbinded !
mediator.on('ready', this.onReady); // No need for explicit 'bind' now !
};
Foo.prototype.onReady = function() {
// mediator.off('ready', this.onReady.bind(this)); // That is sad, bro
mediator.off('ready', this.onReady); // Properly unbinded !
};
It used to be a standalone version of underscore's _.bindAll()
function for IE9+ browsers.
But since bindAll goal is to provide a quick way to bind/unbind methods, and only that, I updated it to use a quicker, more compatible bind
function.
See the underscore source for reference.
Basically, it avoids this use case:
mediator.on('foo', this.bar.bind(this));
mediator.off('foo', this.bar.bind(this));
// will never be unbinded because this.bar.bind(this) != this.bar.bind(this)
Since bindAll's only goal is to bind a method to its object context, the bind function can be written as:
function bind(func, context) {
return function() {
return func.apply(context, arguments);
};
}
No need for the modern bind
function, no need for a polyfill.
And it's a lot faster ! Check this jsPerf case.
There is one significant thing to know: by binding a method on it's object context, it creates an instance-level method. Check this article and this fiddle for more info.
FAQs
Standalone, improved version of underscore's `_.bindAll()` function for IE9+ browsers.
We found that bindall-standalone 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 Go support is now generally available, bringing automatic scanning and deep code analysis to all users with Go projects.
Security News
vlt adds real-time security selectors powered by Socket, enabling developers to query and analyze package risks directly in their dependency graph.
Security News
CISA extended MITRE’s CVE contract by 11 months, avoiding a shutdown but leaving long-term governance and coordination issues unresolved.