Static Shape Counter
Part of the Object Oriented Programming section of Coddy's JavaScript journey. Lesson 42 of 56.
Challenge
Modify the Shape class to track how many shapes have been created using static properties and methods.
Your task:
- Add a static property called
totalShapesinitialized to0 - Increment the counter in the constructor:
Shape.totalShapes++; - Add a static method called
getTotalCreated()that returns the current count of total shapes
Try it yourself
import { Shape } from './Shape.js';
import { Circle } from './Circle.js';
// Tests
console.log(`Initial count: ${Shape.getTotalCreated()}`); // Should be 0
const shape1 = new Shape('red');
const shape2 = new Shape('green');
const circle1 = new Circle('blue', 5);
const circle2 = new Circle('yellow', 10);
console.log(`After creating 4 shapes: ${Shape.getTotalCreated()}`); // Should be 4All 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 InheritanceOverriding & Area MethodStatic Shape Counter9Static Methods & Properties
Class-Level vs. Instance-LevelStatic PropertiesStatic Utility MethodsRecap challengePractice on your own: Online JavaScript compiler