this
inside it as an instance of the class. You can say that this is not a problem when using bind()
. All this is true, but exactly as long as it does not reach the removal of the event handler, since Function::bind()
works in such a way that it returns a new function wrapping the original. Therefore, you need to save the same wrapper somewhere to remove the handler (without removing all the event handlers of the element, which is not very good inside the portable class).el.addEvent('click', myFn.bind(this))
. Thus, it cannot be removed using el.removeEvent('click', myFn.bind(this))
, because bind()
will return a new function that was not used in addEvent()
. The only solution in this case is to remove all element handlers using el.removeEvents('click')
, but this will affect all handlers, including those that could be assigned to third-party classes.var MyClass = new Class({
initialize: function (){
this .bound = {
sayHello: this .sayHello.bind( this ),
sayGoodbye: this .sayGoodbye.bind( this )
}
// this.bound.sayHello
// this.bound.sayGoodbye
},
sayHello: function () { /* ... */ },
sayGoodbye: function () { /* ... */ }
});
this.bound.myFn
whenever I use the this.bound.myFn
functions? And then, I do not want to bind these functions manually each time. After reviewing various solutions, I came to one that is optimal in speed (do not be afraid, this is faster than the solution above) and convenience.Binds
class mutator”. Many of you may not have heard of class mutators before, but have definitely used them when writing your classes. Built-in mutators: Implements
and Extends
, and here’s the Binds
code:Class.Mutators.Binds = function (self, methods) {
$splat(methods).each( function (method){
var fn = self[method];
self[method] = function (){
return fn.apply(self, arguments);
};
});
};
Binds: 'foo'
simply bind foo
to the class, Binds: ['foo', 'bar']
bind foo
and bar
. Pretty simple, isn't it?Binds
mutator:var MyClass = new Class({
Binds: [ 'sayHello' , 'sayGoodbye' ],
initialize: function (){
// this.sayHello
// this.sayGoodbye
},
sayHello: function () { /* ... */ },
sayGoodbye: function () { /* ... */ }
});
Source: https://habr.com/ru/post/40059/
All Articles