OnOff極小的 JavaScript 庫(kù)
OnOff 是個(gè)極小的 JavaScript 庫(kù),提供 on(), off() 和 trigger() 事件管理函數(shù)。同時(shí)還包括一個(gè)非常方便的 once() 函數(shù),事件只能調(diào)用一次。
示例:
> // 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
評(píng)論
圖片
表情
