Exercise
Open up your inspector tools in Google Chrome and head over to the console tab. Now create a new object named ‘person’. Taking what we have seen with our animal object, create an object about yourself. Add in your own details as name: value pairs. Once you have finished adding your properties add one single method named ’speak’ which simply outputs to the console ‘Hello!’ Now run person.speak() and confirm you have set up your object correctly.
Well done – you have just learned one of the most important and challenging aspects of JavaScript. Put simply, if you understand objects, you understand a huge part of JavaScript. Congratulations – you can now claim to have an understanding of a programming language.
You’ve done fantastically well to have kept up so far. I know these concepts can seem a little daunting at first, but once you break them down into bitesize chunks, they are actually very tame. Take your time and go at your own pace. Never feel that you can’t look back through the previous chapters to help you achieve and complete the exercises – it’s so important that you understand each of these concepts as much as you can before moving on, as each concept builds on top of the foundations laid by the previous concepts. Objects are so important in JavaScript, so take the time to make sure that you understand them until the point where looking at the syntax feels like second nature. I encourage you to go off and create some other objects. Why not create one for a car or for a musical instrument? Start thinking in a more programmatically minded way about how to transpose real-life objects into a JavaScript object. Getting into the mindset of a programmer is so important on the journey to understanding JavaScript and becoming a fantastic web developer. You’re doing great – keep it up!