Garrett ✊ Smith · @xkit

27th Jun 2011 from Twitlonger

In reply to AVK's idea of changing the behavior of global `Event`.

The problem with that is that it adds complexity to web page authors. It makes feature detection much harder because there are multiple cases to deal with:
1) Browser has native Event interface object (typeof Event == "object" || typeof Event == "function" (new))
2) Browser does not have native Event interface object
(typeof Event == "undefined")
3) App is using a framework that adds/replaces global `Event`
a) Prototype.js
| if (!window.Event) var Event = { }; (typeof Event == "object")
b) MooTools:
| var Event = new Type(... (typeof Event == "function")

That makes it hard to feature test the global Event constructor.

Don't change existing objects' behavior!

For a copy'n'paste coder who tests in three browsers, AVK's changed Event object will probably lead to compatibility bugs.

Now, OTOH, a *new* method can avoid dealing with legacy code issues:

| if(window.Event && typeof window.Event.createInitedEvent == "function") {
| Event.createInitedEvent(type, optionObj)
| }

It should be a safe bet to assume that if `createInitedEvent` is present, it's not something that MooTools added. This avoids compatibility issues. Copy'n'pasters can drop that on into a web page and you, browser vendor, should not see "TypeError Event is not a constructor", etc.

Reply · Report Post