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 110 | Next

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

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

Where this can be especially tricky is if someone has extended the Object with custom
methods. People often do this because it can be really handy. As an example, let??™s create an
object in which you store a number of properties used to page through a set of search results:
var queryComponents = {
sortBy: 'name',
page: 1,
pages: 10,
resultsPerPage: 20
}
CHAPTER 3 n OBJECT-ORIENTED PROGRAMMING 65
function queryBuilder(obj)
{
var querystring = '?';
for(var property in obj)
{
// make sure I have something already appended
// before adding the & to separate values
if(querystring.length > 1) querystring += '&';
querystring += property + '=' + obj[property];
}
return querystring;
}
queryComponents enables you to define your query string in a convenient manner. If you
want 30 results per page instead of 20, just change the value. The queryBuilder function loops
through the properties of the object and builds a query string that can be passed back to the
server. Running queryBuilder(queryComponents) gives you the following output:
?sortBy=name&page=1&pages=10&resultsPerPage=20
However, what happens if somebody adds to Object.prototype?
Object.prototype.extend = function(obj) {
for (var property in obj) {
this[property] = obj[property];
}
return this;
}
This function is pretty handy because it enables you to create a type of inheritance by
copying the methods and properties of one object onto another.


Pages:
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
druga wojna światowa Free English grammar and study guid hotel jelenia góra Russian bride counter strike 1.6