O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required