Social Icons

Pages

Thursday, December 1, 2016

Introduction to Objects I 31/33

Properties
Properties are like variables that belong to an object, and are used to hold pieces of information. Properties can be accessed in two ways:
Dot notation, with ObjectName.PropertyName
Bracket notation, withObjectName["PropertyName"] (don't forget the quotes!)
In the editor, we have brought back oursnoopy object, with a species and ageproperty.

Instructions
Set the global variable species to be snoopy's species and the variable age to be snoopy's age. For one use dot notation and the other use bracket notation!
?
Hint
Remember the notation isObjectName.PropertyName for line 7, with the object name of snoopy and the property name of species. Then useObjectName['PropertyName'] for line 10, with the property name age.


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'];




No comments:

Post a Comment