SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 120 | Next

Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb

"Accelerated DOM Scripting with Ajax, APIs, and Libraries"

getElementById(elId);
if (el)
{
callback();
}else{
polled[polled.length] = {'element':elId, 'callback':callback};
startPoll();
}
}
}
};
CHAPTER 3 n OBJECT-ORIENTED PROGRAMMING 73
This type of encapsulation can also be used to create a class to instantiate a number of
objects in which each object needs access to these hidden properties and methods (called private
members):
function CurrentAnswer(num)
{
var current = num;
var newObject = {
getCurrent: function(){ return 'The current answer is: ' + current; }
}
return newObject;
}
var curr = new CurrentAnswer('5');
alert(curr.getCurrent()); // alert's the string 'The current answer is: 5'
Like the previous example, you can define a solid API with which to interact with your
object and obscure away the inner workings.
Functional Programming
Functional programming is another programming paradigm, just as OOP is a paradigm. It is
the concept of accepting functions as arguments and being able to return functions as a result
(known as higher-order functions). This approach is very powerful, and many JavaScript
libraries take advantage of JavaScript??™s capability to do this.
Callbacks
A callback is the process of passing in a function (or the name of a function) into another
function so that when the code finishes executing, it can ???call that function back.??? Callbacks
are quite common, especially in event-driven scenarios.


Pages:
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132