JavaScript_Succinctly
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 10 Object()<br />
Conceptual overview of using Object() objects<br />
Using the built-in Object() constructor function, we can create generic empty objects<br />
on the fly. In fact, if you remember back to the beginning of Chapter 1, this is exactly<br />
what we did by creating the cody object. Let’s recreate the cody object.<br />
Sample: sample69.html<br />
<br />
var cody = new Object(); // Create an empty object with no properties.<br />
for (key in cody) { // Confirm that cody is an empty generic object.<br />
if (cody.hasOwnProperty(key)) {<br />
console.log(key); // Should not see any logs, because cody itself<br />
has no properties.<br />
}<br />
}<br />
<br />
Here, all we are doing is using the Object() constructor function to create a generic<br />
object called cody. You can think of the Object() constructor as a cookie cutter for<br />
creating empty objects that have no predefined properties or methods (except, of<br />
course, those inherited from the prototype chain).<br />
Notes<br />
If it’s not obvious, the Object() constructor is an object itself. That is, the constructor<br />
function is based on an object created from the Function constructor. This can be<br />
confusing. Just remember that like the Array constructor, the Object constructor<br />
simply spits out blank objects. And yes, you can create all the empty objects you like.<br />
However, creating an empty object like cody is very different than creating your own<br />
constructor function with predefined properties. Make sure you understand that cody is<br />
just an empty object based on the Object() constructor. To really harness the power of<br />
<strong>JavaScript</strong>, you will need to learn not only how to create empty object containers from<br />
Object(), but also how to build your own "class" of objects (e.g., Person()) like the<br />
Object() constructor function itself.<br />
83