Something went wrong. Try again.
javascript for 🐜's, a tiny runtime with big ambitions antjs.org
Something went wrong. Try again.
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158// Test private fields in classes
// Basic private field accessclass Rectangle { #width; #height; constructor(w, h) { this.#width = w; this.#height = h; } area() { return this.#width * this.#height; }}
const rect = new Rectangle(5, 10);console.assert(rect.area() === 50, "Rectangle area should be 50");
// Private field with getter/setterclass Person { #name; #age; constructor(name, age) { this.#name = name; this.#age = age; } getName() { return this.#name; } getAge() { return this.#age; } setAge(newAge) { this.#age = newAge; } describe() { return this.#name + " is " + this.#age; }}
const person = new Person("Alice", 30);console.assert(person.getName() === "Alice", "getName should return Alice");console.assert(person.getAge() === 30, "getAge should return 30");console.assert(person.describe() === "Alice is 30", "describe should work");person.setAge(31);console.assert(person.describe() === "Alice is 31", "setAge should work");
// Private fields with inheritanceclass Animal { #species; constructor(species) { this.#species = species; } getSpecies() { return this.#species; }}
class Dog extends Animal { #name; constructor(name, species) { super(species); this.#name = name; } getName() { return this.#name; }}
const dog = new Dog("Rex", "Canis familiaris");console.assert(dog.getName() === "Rex", "Dog getName should return Rex");console.assert(dog.getSpecies() === "Canis familiaris", "Dog getSpecies should work");
// Private field with initializerclass Counter { #count = 0; constructor(start) { this.#count = start; } increment() { this.#count = this.#count + 1; return this.#count; } getCount() { return this.#count; }}
const counter = new Counter(10);console.assert(counter.increment() === 11, "Counter increment should return 11");console.assert(counter.increment() === 12, "Counter increment should return 12");console.assert(counter.getCount() === 12, "Counter getCount should return 12");
// Performance test: tight loop with private fields and initializerclass FastCounter { #n = 0; increment() { this.#n = this.#n + 1; } get() { return this.#n; }}
const fast = new FastCounter();for (let i = 0; i < 1000; i = i + 1) { fast.increment();}console.assert(fast.get() === 1000, "FastCounter should reach 1000");
// Private fields may use keyword-like namesclass KeywordPrivate { #var = 7; #default = 9;
sum() { return this.#var + this.#default; }}
const keywordPrivate = new KeywordPrivate();console.assert(keywordPrivate.sum() === 16, "Keyword-named private fields should parse and work");
// Class fields named get/set should remain fields, not accessorsclass FieldNamedAccessor { #value = 0;
set = (value) => { this.#value = value; };
get = () => { return this.#value; };}
const fieldNamedAccessor = new FieldNamedAccessor();fieldNamedAccessor.set(23);console.assert(fieldNamedAccessor.get() === 23, "Field names get/set should support arrow initializers");
console.log("All private fields tests passed!");