We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

1man on 09/04/07


Tagged

javascript object


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

arcturus
vali29


Basic Object Creation


Published in: JavaScript 


Create a simple object and add properties and a method. Note where the object is parsed (not from top to bottom).

  1. function mooTutorial() {
  2. var city = { //create the city object
  3. numberOfCars : 0, //define number of cars property
  4. hasTraffic: true, //define if it has traffic
  5. grow: function(howMany) { //creates a grow() method. looks for variable howMany.
  6. this.numberOfCars += howMany; //adds howMany to numberOfCars.
  7. }
  8. }//This is where the object gets parsed, order of members is unimportant.
  9. console.info(city.numberOfCars); //outputs 0
  10. city.grow(10);
  11. console.info(city.numberOfCars); //outputs 10
  12. }
  13. window.onload = mooTutorial();

Report this snippet 

You need to login to post a comment.