Modernizr
Modernizr copied to clipboard
Let .hasEvent work with array iteration methods.
This simple change lets .hasEvent(event, element?)
work with methods like array.every
or array.filter
. It checks if element
is a number and if so it uses this
as the element
.
['drag', 'dragend'].every(Modernizr.hasEvent) // uses 'div' in strict mode, `window` in non-strict
['drag', 'dragend'].every(Modernizr.hasEvent, 'img') // creates element on each iteration
['drag', 'dragend'].every(Modernizr.hasEvent, document.createElement('img')) // reuses element
Explicitly supplying the thisArg avoids the strict mode difference.
Hi Ryan, sorry it’s taken a while to get to this – looks good.
I don’t get why there’s a difference between strict mode / non-strict mode though? My knowledge of the details of strict mode is embarrassingly lacking though, tbh.
I accounted for both modes in case 'use strict';
was ever added here or in an outer closure.
strict mode
- No boxing occurs. thisArg values resolve exactly.
this
defaults toundefined
unstrict mode
-
this
resolves towindow
when thisArg isundefined
ornull
- Primitive thisArg values get boxed into the
"object"
type..valueOf()
unboxes them (if primitive) but throws if used onundefined
|null
|Object.create(null)
Consider the docs, and keeping them simple. .hasEvent
can be used with array methods by passing an element or tagname as the thisArg. Then show examples. Questions to answer are:
- Should we add the syntax at all? (It's not needed, but it helps write terse code)
- Should it support tagnames? (If no then the first line of variant 3 would suffice)
- Should it worry about strict mode? (I like variant 1 if yes, and my original or 2 if no)