I’d like to make it possible to use an onComplete event in my MooTools class by implementing Events and using this.fireEvent("complete"). By default I just want to let the class do nothing. So how would you realize the default “do nothing” thing the cleanest and safest way?
Should I just omit the option onComplete in the default class options object and still use this.fireEvent("complete") in the context? Or is this causing errors or something? I just did it that way all the time, but I’m not sure if this is the best way…
Would you use something like if(this.options.onComplete && instanceOf(this.options.onComplete, Function)) { this.fireEvent("complete") } instead? Or would you specify an empty function as default like options: { onComplete: function(){} }?
I don’t care about the shortest code at this point but I was wondering how to do it the most conform and memory saving way.
Thanks for your recommendations! 🙂
Good question. Have a look at the source code of MooTools:
As you can see, the fireEvent will just return the current instance when an event is not found. Omitting
onCompleteand keepingthis.fireEvent('complete')would therefore not give any errors or weird behaviour.I would not even specify an empty function in the options. As you can see here, the events are simply left undefined and only mentioned using a comment so people know they are present and can be used: