const CarModule = (function(){
// symbols hidden in a IIFE
const speedKey = Symbol("speedKey");
const milesDrivenKey = Symbol("milesDrivenKey");
return class CarModule {
constructor() {
this[speedKey] = 0;
this[milesDrivenKey] = 0;
}
accelerate(amount) {
this[speedKey] += amount;
this[milesDrivenKey] += this[speedKey];
}
getMilesDriven() {
return this[milesDrivenKey];
}
}
})();
const testCarModule = new CarModule();
testCarModule.accelerate(5);
testCarModule.accelerate(4);
function log(...args) {
console.log(...args);
const pre = document.createElement('pre');
pre.innerHTML = JSON.stringify(args);
document.body.appendChild(pre);
}
console.clear();
log(testCarModule.getMilesDriven());
log(testCarModule.speed); // => undefined
log(Object.keys(testCarModule)); // => []
log(speedKey); // ReferenceError
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.