-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Description
Many jQuery collection operations are only meaningful on elements or (somewhat less commonly) DOM nodes, but the collections can logically contain anything. Such operations should always assume valid input, potentially throwing exceptions when they encounter an invalid member—especially given the ease of manually cleaning like $collection.filter("*").
- Single-member read (e.g.,
.attr( name ),.css( name ),.offset()) - Whole-collection read (e.g.,
.hasClass( clazz )) - Whole-collection write (e.g.,
.attr( name, val ),.css( props ),.offset( n ),.addClass( clazz ))
Special consideration is warranted for single-member reads responding to empty collections. Current behavior usually returns undefined, which seems reasonable to standardize on (including changing those methods that return null, like .scrollTop() and .width()). There are cases where this collides with real output, but such collisions seem to be sane—$el.css( unknownProperty ), $el.prop( nonexistent ), $hasNoData.data(), etc.
Related: