Creating Objects

  var obj1 = {name:"Brad", title:"Author", "last-name":"Dayley" };   var obj2 = new Object();   obj2.name = "Frodo";   obj2.title = "Hobbit";   obj2["last-name"] = "Baggins";

You can create objects in the definition using {property:value, ..} syntax, such as var obj={name:"Brad", title:"Author"};. You can also create a new object and add properties to it using standard dot syntax.

The following code snippet shows an example of creating an array in each of these ways.

<script>   var obj1 = {name:"Brad", title:"Author", "last-name":"Dayley" };   var obj2 = new Object();   obj2.name = "Frodo";   obj2.title = "Hobbit";   obj2["last-name"] = "Baggins";   document.write(obj1.name ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.