Smart Home Device System
Part of the Object Oriented Programming section of Coddy's JavaScript journey — lesson 56 of 56.
Challenge
In this challenge you have to add static tracking to the SmartDevice system so we can monitor how many devices are in our smart home.
1. Add static property to SmartDevice class:
- Add:
static totalDevices = 0;at the top of the class
2. Increment the counter in constructor:
- In the
SmartDeviceconstructor, add:SmartDevice.totalDevices++;
3. Add static method to get count:
- Add:
static getDeviceCount() { return SmartDevice.totalDevices; }
Try it yourself
import { SmartDevice } from './device.js';
console.log(`Device count: ${SmartDevice.getDeviceCount()}`); // Should be 0
const basicDevice1 = new SmartDevice('Smart Plug');
const basicDevice2 = new SmartDevice(' Thermostat');
console.log(`Total devices now: ${SmartDevice.getDeviceCount()}`); // Should be 2
All lessons in Object Oriented Programming
1Objects & The this Keyword
Quick Review: ObjectsAdding Methods to ObjectsUnderstanding the this KeywordConstructor FunctionsThe new KeywordRecap Challenge7 Inheritance & The extends Key
InheritanceThe "is-a" RelationshipThe extends KeywordThe super() MethodInheriting Properties&MethodsRecap Challenge2Organizing Code
What are Modules?Exporting with exportImporting with importDefault vs. Named Exports8Organizing OOP Code
Organize Classes into Modules11Project: A Shape Renderer
Setup: Shape Class & ExportCircle Class Inheritance9Static Methods & Properties
Class-Level vs. Instance-LevelStatic PropertiesStatic Utility MethodsRecap challenge12Getters & Setters
The get and set KeywordsComputed PropertiesValidation and Side EffectsRecap Challenge15 Final Challenges
Game Character BuilderE-commerce Product SystemUniversity Management SystemSmart Home Device System