Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created May 3, 2009 16:58
Show Gist options
  • Select an option

  • Save subtleGradient/106058 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/106058 to your computer and use it in GitHub Desktop.
A better Function binds idiom?
// Unnecessary if you use MooTools or anything that already has a Function.prototype.bind method
Function.prototype.bind = Function.prototype.bind || function(bind, args){
var self = this;
if (args != null) args = Array.from(args);
return function(){
return self.apply(bind, args || arguments);
};
};
Class.Mutators.Binds = function(self, binds) {
if (!self.Binds) return self;
delete self.Binds;
var oldInit = self.initialize || function(){};
self.initialize = function() {
for (var i=0, binder; binder = binds[i]; i++){
this[binder].setBind('default', this);
}
return oldInit.apply(this,arguments);
};
return self;
};
Function.prototype.setBind = function(name, bind, args){
this._bindings = this._bindings || {};
this._bindings[name] = this.bind(bind, args);
this._bindings[name].displayName = (this.displayName||'') + ' Bound: "'+ name +'"';
return this;
};
Function.prototype.getBind = function(name){
name = name || 'default';
if (typeof this._bindings != 'object') return null;
return this._bindings[name];
};
/*
How to bind a function to an object manually
*/
function myFunkyFunction(){
alert(this.innerHTML);
};
(function(){
var div1 = document.createElement('div'); document.body.appendChild(div1); div1.innerHTML = 'myDiv 1';
var div2 = document.createElement('div'); document.body.appendChild(div2); div2.innerHTML = 'myDiv 2';
var randomObject = { innerHTML: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' };
myFunkyFunction.setBind('binding 1', div2);
myFunkyFunction.setBind('binding 2', randomObject);
div1.onclick = myFunkyFunction.getBind('binding 1');
div2.onclick = myFunkyFunction.getBind('binding 2');
})();
/*
@require 'mootools'
How to bind a method to your class manually inline
*/
var MyClass = new Class({
foo:'MyClass',
method: function(){ return alert(this.foo); }
});
var instance = new MyClass();
window.foo = 'window';
document.foo = 'document';
instance.method.setBind('doc', document).getBind('doc')(); // alerts "document"
window.addEvent('mousedown', instance.method); // alerts "window"
document.addEvent('mouseup', instance.method.setBind('bar', instance).getBind('bar')); // alerts "MyClass"
/*
@require 'mootools'
How to bind a method to your class using a Binds mutator!
*/
var MyClass = new Class({
Binds: ['method'],
foo:'MyClass',
method: function(){ return alert(this.foo); }
});
document.foo = 'document';
var instance = new MyClass(); // instantiate your class
instance.method(); // normal method // alerts 'MyClass'
instance.method.getBind()(); // default bound method // alerts 'MyClass'
instance.method.getBind('default')(); // default bound method // alerts 'MyClass'
document.addEvent('click', instance.method); // method not bound // alerts 'document'
document.addEvent('click', instance.method.getBind()); // bound method // alerts 'MyClass'
document.removeEvent('click', instance.method.getBind()); // able to remove directly
document.addEvent('click', instance.method.bind(instance)); // "standard" way of adding a bound method
document.removeEvent('click', instance.method.bind(instance)); // can't be removed directly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment