OnOff极小的 JavaScript 库
OnOff 是个极小的 JavaScript 库,提供 on(), off() 和 trigger() 事件管理函数。同时还包括一个非常方便的 once() 函数,事件只能调用一次。
示例:
> // Create an instance of OnOff() > EventManager = new OnOff(); // NOTE: "new" is actually optional > // A little test function: > var testFunc = function(args) { console.log('args: ' + args + ', this.foo: ' + this.foo) }; > // Call testFunc() whenever "test_event" is triggered: > EventManager.on("test_event", testFunc); > // Fire the test_event with 'an argument' as the only argument: > EventManager.trigger("test_event", 'an argument');args: an argument, this.foo: undefined > // Remove the event so we can change it:> EventManager.off("test_event", testFunc); > // Now let's pass in a context object:> EventManager.on("test_event", testFunc, {'foo': 'bar'}); > // Now fire it just like before> EventManager.trigger("test_event", 'an argument');args: an argument, this.foo: bar
评论