MooTools 的优点之一是它可以让您轻松地为对象分配/触发事件,例如:
var playerSingleton = new (new Class({
Implements: [Events],
initialize: function() {},
setVolume: function() {
// do some stuff..
this.fireEvent('volumeChanged')
}
}));
// Somewhere else...
playerSingleton.addEvent('volumeChanged', function() {
// do something when volume changes
});
playerSingleton.setVolume(75);
// bam our event fires.
像这样的事情如何用 jQuery 来完成?我知道有.bindand .trigger,但似乎唯一的方法是将事件绑定/触发到 window 对象:
$(window).bind('volumeChanged', fn);
还有什么比这更好的,更像 MooTools 的方法吗?