Something went wrong. Try again.
MIRROR: javascript for 🐜's, a tiny runtime with big ambitions ant.themackabu.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191// Test class keyword
console.log("=== Testing class keyword ===");
// Test 1: Simple class with constructorclass Person { constructor(name, age) { this.name = name; this.age = age; }}
let person1 = new Person("Alice", 30);console.log(person1.name); // "Alice"console.log(person1.age); // 30
// Test 2: Class with methodsclass Calculator { constructor(value) { this.value = value; } add(n) { this.value = this.value + n; return this.value; } subtract(n) { this.value = this.value - n; return this.value; } getValue() { return this.value; }}
let calc = new Calculator(10);console.log(calc.value); // 10console.log(calc.add(5)); // 15console.log(calc.subtract(3)); // 12console.log(calc.getValue()); // 12
// Test 3: Class without explicit constructorclass Point { setCoords(x, y) { this.x = x; this.y = y; } getDistance() { return this.x * this.x + this.y * this.y; }}
let p = new Point();p.setCoords(3, 4);console.log(p.x); // 3console.log(p.y); // 4console.log(p.getDistance()); // 25
// Test 4: Multiple instancesclass Counter { constructor(start) { this.count = start; } increment() { this.count = this.count + 1; return this.count; }}
let counter1 = new Counter(0);let counter2 = new Counter(10);console.log(counter1.increment()); // 1console.log(counter1.increment()); // 2console.log(counter2.increment()); // 11console.log(counter2.increment()); // 12
// Test 5: Class instances are objectslet person2 = new Person("Bob", 25);console.log(typeof person2); // objectconsole.log(person2 instanceof Object); // true
// Test 6: Modify instance propertiesclass Rectangle { constructor(width, height) { this.width = width; this.height = height; } area() { return this.width * this.height; }}
let rect = new Rectangle(5, 10);console.log(rect.area()); // 50rect.width = 8;console.log(rect.area()); // 80
// Test 7: Delete class instance propertieslet person3 = new Person("Charlie", 35);console.log(person3.name); // "Charlie"delete person3.name;console.log(person3.name); // undefinedconsole.log(person3.age); // 35
// Test 8: Class with property management class Store { constructor() { this.items = 0; } addItem(name, value) { this.items = this.items + 1; } removeItem(name) { this.items = this.items - 1; return true; } getItemCount() { return this.items; }}
let store = new Store();console.log(store.getItemCount()); // 0store.addItem("apple", 5);store.addItem("banana", 3);console.log(store.getItemCount()); // 2store.removeItem("apple");console.log(store.getItemCount()); // 1
// Test 9: Method chainingclass ChainableCounter { constructor(value) { this.value = value; } add(n) { this.value = this.value + n; return this; } multiply(n) { this.value = this.value * n; return this; } get() { return this.value; }}
let chain = new ChainableCounter(5);let result = chain.add(3).multiply(2).get();console.log(result); // 16
// Test 10: Class with boolean propertiesclass Feature { constructor(name) { this.name = name; this.enabled = false; } enable() { this.enabled = true; } disable() { this.enabled = false; } isEnabled() { return this.enabled; }}
let feature = new Feature("DarkMode");console.log(feature.isEnabled()); // falsefeature.enable();console.log(feature.isEnabled()); // truefeature.disable();console.log(feature.isEnabled()); // false
console.log("=== Class tests completed ===");