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

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

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


Keep in mind that nothing is impossible in JavaScript. If you want to create a new object
from that object literal, you can use the following:
function object(o)
{
function F(){}
F.prototype = o;
return new F();
}
var newObject = object(ElementReady);
CHAPTER 3 n OBJECT-ORIENTED PROGRAMMING 64
The object() function accepts an object as an argument. It creates a new function and
attaches the object to the prototype of that object, essentially copying all the properties and
methods to that new object. From there, a new object is instantiated and returned. Credit for
this little function goes to Douglas Crockford.
The for..in Loop
I mentioned the for..in loop earlier, but I want to discuss it in a little more detail because
there are some things to consider when using it. If you??™re not familiar with the for..in loop,
it??™s much like the regular for loop, but it enables you to loop through an object??™s properties,
which act like an associative array:
// our object that we'll loop through
var coreMethods = {
add:function(b){
return a + b;
},
minus:function(b){
return a - b;
},
multiply:function(b){
return a * b;
},
divide:function(b){
return a / b;
}
};
for (var property in coreMethods) {
alert(coreMethods[property]); // alerts each of the functions
}
The for..in loop will loop through methods and properties on the object and on the
prototype. The variable before the in (property, in this case) gets populated with the key
name.


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