WeakMap Practice
by stevenkaspar
JavaScript
'use strict';
let map = new WeakMap();
class Piece {
constructor(health){
map.set(this, {
health: health
});
Object.defineProperty(this, 'attack', {
value: this.attack.bind(this),
writable: false
})
}
get health(){
return map.get(this).health;
}
set health(value){
const starting_health = this.health;
const diff = value - starting_health;
console.log(diff);
for(var i = 0; i < diff; i++){
console.log(i);
map.get(this).health += 1;
}
}
attack(){
console.log('attack');
}
}
var p = new Piece(100);
console.log(p.health);
p.health = 150;
console.log(p.health);
p.attack();