/ Published in: JavaScript

Create a simple object and add properties and a method. Note where the object is parsed (not from top to bottom).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function mooTutorial() { var city = { //create the city object numberOfCars : 0, //define number of cars property hasTraffic: true, //define if it has traffic grow: function(howMany) { //creates a grow() method. looks for variable howMany. this.numberOfCars += howMany; //adds howMany to numberOfCars. } }//This is where the object gets parsed, order of members is unimportant. console.info(city.numberOfCars); //outputs 0 city.grow(10); console.info(city.numberOfCars); //outputs 10 } window.onload = mooTutorial();
Comments
