Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

3. Object() > Conceptual Overview of Using Object() Objects

Conceptual Overview of Using Object() Objects

Using the built-in Object() constructor function, we can create generic empty objects on the fly. In fact, if you remember back to the beginning of Chapter 1, this is exactly what we did by creating the cody object. Let’s re-create the cody object.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var cody = new Object(); // create an empty object with no properties

for (key in cody) { // confirm that cody is an empty generic object
   if(cody.hasOwnProperty(key)) {
       console.log(key); /* should not see any logs, 
                         because cody itself has no properties */
   }
}

</script></body></html>

Here, all we are doing is using the Object() constructor function to create a generic object called cody. You can think of the Object() constructor as a cookie cutter for creating empty objects that have no predefined properties or methods (except, of course, those inherited from the prototype chain).


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint