jQuery in Action
334
APPENDIX
JavaScript that you need to know but might not!
Employing the
call()
method of
Function
causes the function context to
be set to whatever object is passed as the first parameter to
call()
--in this
case,
o2
g
. In this example, the function acts like a method to
o2
, even
though it has no association whatsoever--even as a property--with
o2
.
As with
call()
, using the
apply()
method of
Function
sets the function con-
text to whatever object is passed as the first parameter
h
. The difference
between these two methods only becomes significant when parameters are
passed to the function (which we didn't do in this example for simplicity).
passed to the function (which we didn't do in this example for simplicity).
This example page clearly demonstrates that the function context is determined
on a per invocation basis and that a single function can be called with any object
acting as its context. It's, therefore, probably never correct to say that a function is
a method of an object. It's much more correct to state the following:
on a per invocation basis and that a single function can be called with any object
acting as its context. It's, therefore, probably never correct to say that a function is
a method of an object. It's much more correct to state the following:
A function f acts as a method of object o when o serves as the function context of the
invocation of f.
invocation of f.
As a further illustration of this concept, consider the effect of adding the follow-
ing statement to our example:
ing statement to our example:
alert(o1.identifyMe.call(o3));
Even though we reference the function as a property of
o1
, the function context
for this invocation is
o3
, further emphasizing that it's not how a function is
declared but how it's invoked that determines its function context.
When using jQuery commands and functions that employ callbacks, this
proves to be an important concept. We saw this concept in action early on (even if
you didn't realize it at the time) in section 2.3.3 where we supplied a callback
function to the
you didn't realize it at the time) in section 2.3.3 where we supplied a callback
function to the
filter()
method of
$
and that function was sequentially invoked
with each element of the wrapped set serving as its function context in turn.
Now that we understand how functions can act as methods of objects, let's turn
our attention to another advanced function topic that will play an important role
in effective usage of jQuery--closures.
in effective usage of jQuery--closures.
A.2.4 Closures
To page authors coming from a traditional
OO
or procedural programming back-
ground, closures are often an odd concept to grasp; whereas, to those with a func-
tional programming background, they're a familiar and cozy concept. For the
uninitiated, let's answer the question: What are closures?
tional programming background, they're a familiar and cozy concept. For the
uninitiated, let's answer the question: What are closures?
Stated as simply as possible, a closure is a
Function
instance coupled with the
local variables from its environment that are necessary for its execution.