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() > Creating Object() Objects Using “Object Literals”

Creating Object() Objects Using “Object Literals”

Creating an “object literal” entails instantiating an object with or without properties using braces (e.g., var cody = {};). Remember back to the beginning of Chapter 1, when we created the one-off cody object and then gave the cody object properties using dot notation? Let’s do that again.

Live Code

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

var cody = new Object();
cody.living = true;
cody.age = 33;
cody.gender = 'male';
cody.getGender = function() {return cody.gender;};

console.log(cody); // logs cody object and properties

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

Notice in the code above that creating the cody object and its properties took five statements. Using the “object literal” notation, we can express the same cody object in one statement.


  

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