Modernizr icon indicating copy to clipboard operation
Modernizr copied to clipboard

Let .hasEvent work with array iteration methods.

Open ryanve opened this issue 11 years ago • 3 comments

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.

ryanve avatar Jan 08 '14 12:01 ryanve

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.

stucox avatar Feb 23 '14 12:02 stucox

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 to undefined

unstrict mode

  • this resolves to window when thisArg is undefined or null
  • Primitive thisArg values get boxed into the "object" type. .valueOf() unboxes them (if primitive) but throws if used on undefined|null|Object.create(null)

ryanve avatar Feb 24 '14 02:02 ryanve

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)

ryanve avatar Feb 24 '14 05:02 ryanve