jQuery in Action
Manipulating JavaScript objects and collections
177
A pattern for mimicking inheritance in JavaScript is to extend an object by
copying the properties of a base object into the new object, extending the new
object with the capabilities of the base.
object with the capabilities of the base.
NOTE
If you're an aficionado of object-oriented JavaScript, you'll no doubt be
familiar with extending not only object instances but also their blueprints
via the
familiar with extending not only object instances but also their blueprints
via the
prototype
property of object constructors.
$.extend()
can be
used to effect such constructor-based inheritance by extending
proto-
type
, as well as object-based inheritance by extending existing object
instances. Because understanding such advanced topics isn't a require-
ment in order to use jQuery effectively, this is a subject--albeit an impor-
tant one--that's beyond the scope of this book.
ment in order to use jQuery effectively, this is a subject--albeit an impor-
tant one--that's beyond the scope of this book.
It's fairly easy to write JavaScript code to perform this extension by copy, but as
with so many other procedures, jQuery anticipates this need and provides a
ready-made utility function to help us out. As we'll see in the next chapter, this
function is useful for much more than extending an object, but even so its name is
with so many other procedures, jQuery anticipates this need and provides a
ready-made utility function to help us out. As we'll see in the next chapter, this
function is useful for much more than extending an object, but even so its name is
$.extend()
. Its syntax is as follows:
Function syntax: $.extend
$.extend(target,source1,source2, ... sourceN)
Extends the object passed as target with the properties of the remaining passed objects.
Parameters
target
(Object) The object whose properties are augmented with the
properties of the source objects. This object is directly modi-
fied with the new properties before being returned as the value
of the function.
Any properties with the same name as properties in any of the
source elements are overridden with the values from the
source elements.
properties of the source objects. This object is directly modi-
fied with the new properties before being returned as the value
of the function.
Any properties with the same name as properties in any of the
source elements are overridden with the values from the
source elements.
source1 ... sourceN
(Object) One or more objects whose properties are added to
the target object.
When more than one source is provided and properties with
the same name exist in the sources, sources later in the argu-
ment list take precedence over those earlier in the list.
the target object.
When more than one source is provided and properties with
the same name exist in the sources, sources later in the argu-
ment list take precedence over those earlier in the list.
Returns
The extended target object.
The extended target object.