2012-05-06 10:47:01Morris

[JavaScript] Introduction to Objects I

Objects In Review

var spencer = {
  age: 22,
  country: "United States"
};

// make spencer2 here with constructor notation
var spencer2 = new Object();
spencer2.age = 22;
spencer2.country = "United States";


var snoopy = new Object();
snoopy.species = "beagle";
snoopy.age = 10;

// save Snoopy's age and species into variables
// use dot notation for snoopy's species
var species = snoopy.species;
   
// use bracket notation for snoopy's age
var age = snoopy.age;


function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        return Math.PI * this.radius * this.radius;
        
    };
    // define a perimeter method here
    this.perimeter = function() {
        return Math.PI * 2 * this.radius;
    }
};


// 3 lines required to make harry_potter
var harry_potter = new Object();
harry_potter.length = 350;
harry_potter.author = "J.K. Rowling";

// A custom constructor for book
function Book (length, author) {
    this.length = length;
    this.author = author;
}

// Use our new constructor to make the_hobbit in one line
var the_hobbit = new Book(320, "J.R.R. Tolkien");