#Outline for Monday 9/21
- Review Wednesday's Exercise
- Objects
##Review Wednesday's Material
Similar to an Array
, an Object
is a set of key
/value
pairs, with each key having to be unique. Variables inside an object are known as properties
, while functions are known as 'methods'. The value of a property can be a string, number, boolean, array or another object. The value of a method is always a function.
But it will all make sense once you get started!
##Let's model an Object!! Group modeling of object.
###Creating an Object using Literal Notation
var hotel = {
name: 'Hilton',
rooms: 120,
booked: 93,
pool: true,
roomTypes: ['twin', 'full', 'queen', 'king'],
//we will cover functions in greater depth next week
availability: function() {
return this.rooms - this.booked;
}
};
###Creating an Object using Constructor Notation
var hotel = new Object();
//properties
hotel.name = 'Best Western';
hotel.rooms = 65;
hotel.booked = 34;
//method
hotel.availability = function() {
return this.rooms - this.booked;
};
###Accessing an Object
The properties and methods of an object can be accessed using dot notation
. Properties can also be accessed using square brackets.
var hotelName = hotel.name;
// Hilton
var availableRooms = hotel.availability;
// 27
var hotelName = hotel['name'];
// Hilton
###Updating and adding to an Object To add to or update the properties of an object, dot notation or square brackets can be used.
hotel.pool = false; //sets the pool property to false
hotel.lounge = true; //adds a new lounge property with a value of true
hotel;
// {name: 'Hilton', rooms: 120, booked: 93, pool: false, lounge: true, availability: function(){....};}
###Deleting a property
Use the 'delete' keyword followed by the object and property names. To clear the value of a property without deleting it, set its value to a blank string ''
delete hotel.pool; //deletes the pool property from the hotel object
hotel.name = ''; //clears the value
##In Class Exercise Creating and manipulating objects.