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

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

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

But now when you run your
queryBuilder function, you get this result:
?sortBy=name&page=1&pages=10&resultsPerPage=20&extend=function (obj) {
for (var property in obj) {
this[property] = obj[property];
}
return this;
}
To check for properties that belong only to the object at hand, use the hasOwnProperty
method. Here??™s the rewritten queryBuilder function that demonstrates it (the new addition is
highlighted in bold):
CHAPTER 3 n OBJECT-ORIENTED PROGRAMMING 66
function queryBuilder(obj)
{
var querystring = '?';
for(var property in obj)
{
if(obj.hasOwnProperty(property))
{
// make sure I have something already appended
// before adding the & to separate values
if(querystring.length > 1) querystring += '&';
querystring += property + '=' + obj[property];
}
}
return querystring;
}
As you loop through all the properties, check to make sure that the property belongs
directly to the object and isn??™t from the prototype. When using a for..in loop, it??™s good practice
to always check for hasOwnProperty.
Named Parameters
The object literal can be a handy way to handle named and optional arguments in JavaScript,
as well as being able to easily define default options for an object. When you declare a function,
you normally specify a number of arguments as options for that function. If anything is
forgotten, it is simply passed through as undefined:
function func(a, b, c)
{
alert(a); //undefined
}
func();
Likewise, if you care about only the first and third arguments, you still have to pass something
in for the second parameter.


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