<p>This is the JavaScrip code I wrote for the exercise, Build a New Object and the output to the console that it generates. The code creates two objects, a microphone and a cd.</p>
<p>The microphone object is relatively simple, it has four properties and one method which accepts a string of text and "records" it by assigning it to the text property. Incidentally, I had just purchased a new microphone when I was writing this code, so the object is based on that new microphone.</p>
<p>The cd object is actually more complex although it doesn't have any methods. It also has four properties, but one of these, tracks, is an object which is made up of 15 separate objects. Each of the 15 objects is an individiual track with two properties, title and writer. This is not the best way to code an object like this, in all likelihood, the tracks would be an array of tracks rather than indovidual objects, but this method is more in keeping with this stage of the course. Arrays will be covered in chapter 7.</p>
<h2class="sectiontitle">The JavaScript Code</h2>
<preclass="codesample">
/**
* Practice: Building objects
*
* - Create JavaScript objects based on objects in your current environment.
* - Give each object an identifiable name.
* - Create properties to describe the objects and set their values.
* - Find an object that has another object inside of it to create a nested object.
* - Test your objects in the browser console by accessing the entire object and its specific properties.
*/
const microphone = {
brand: "Blue",
colour: "Silver",
price: 92.65,
text: "",
record: function (speech) {
this.text = speech;
},
};
const cd = {
title: "One Step Beyond",
artist: "Madness",
release: 1979,
tracks: {
track1: {
title: "One Step Beyond",
writer: "Cecil Campbell",
},
track2: {
title: "My Girl",
writer: "Mike Barson",
},
track3: {
title: "Night Boat to Cairo",
writer: "Graham McPherson, Mike Barson",
},
track4: {
title: "Believe Me",
writer: "John Hasler, Mike Barson",
},
track5: {
title: "Land of Hope and Glory",
writer: "Lee Jay Thompson",
},
track6: {
title: "The Prince",
writer: "Lee Jay Thompson",
},
track7: {
title: "Tarzan's Nuts",
writer: "Chas Smash, Mike Barson, Sydney Lee",
},
track8: {
title: "In the Middle of the Night",
writer: "Chris Foreman, Graham McPherson",
},
track9: {
title: "Bed and Breakfast Man",
writer: "Mike Barson",
},
track10: {
title: "Razor Blade Alley",
writer: "Lee Jay Thompson",
},
track11: {
title: "Swan Lake",
writer: "Pyotr Ilyich Tchaikovsky; arranged by Barson",