jQuery in Action
176
CHAPTER 6
jQuery utility functions
This function is intended for use in code that doesn't make much use of jQuery,
which internally handles this sort of thing on our behalves. This function also
comes in handy when dealing with
which internally handles this sort of thing on our behalves. This function also
comes in handy when dealing with
NodeList
objects while traversing
XML
docu-
ments without jQuery.
Another seldom-used function that might come in handy when dealing with
arrays built outside of jQuery is the
$.unique()
function.
Again, this is a function that jQuery uses internally to ensure that the lists of ele-
ments that we receive contain unique elements, and is intended for use on
element arrays created outside the bounds of jQuery.
ments that we receive contain unique elements, and is intended for use on
element arrays created outside the bounds of jQuery.
Now that we've seen how jQuery helps us to easily work with arrays, let's see a
way that it helps us manipulate plain old JavaScript objects..
6.3.6 Extending objects
Although we all know that JavaScript provides some features that make it act in
many ways like an object-oriented language, we know that JavaScript isn't what
anyone would call purely object-oriented because of the features that it doesn't
support. One of these important features is inheritance--the manner in which new
classes are defined by extending the definitions of existing classes.
many ways like an object-oriented language, we know that JavaScript isn't what
anyone would call purely object-oriented because of the features that it doesn't
support. One of these important features is inheritance--the manner in which new
classes are defined by extending the definitions of existing classes.
Function syntax: $.makeArray
$.makeArray(object)
Converts the passed array-like object into a JavaScript array
Parameters
object
(Object) The array-like object (such as a NodeList) to be converted
Returns
The resulting JavaScript array
The resulting JavaScript array
Function syntax: $.unique
$.unique(array)
Given an array of DOM elements, returns an array of the unique elements in the original array
Parameters
array
(Array) The array of DOM elements to be examined
Returns
An array of DOM elements consisting of the unique elements in the passed array
An array of DOM elements consisting of the unique elements in the passed array