Можно написать что-то такое с использованием прототипов:
class Filters {
constructor(x) {
this.default = { prop: 'foo' };
this.list = {};
}
add(name) {
this.list[name] = Object.create(this.default);
}
}
let attempt = new Filters();
attempt.add('first');
attempt.add('second');
attempt.list['second'].prop = 'custom';
console.log(attempt.list['first'].prop, attempt.list['second'].prop);
// → foo custom
attempt.default.prop = 'bar';
console.log(attempt.list['first'].prop, attempt.list['second'].prop);
// → bar custom
Или поразвлекатся с
Object.definePropertyclass Filters {
constructor(x) {
this.prop = 'foo';
this.list = {};
}
add(name) {
const item = this.list[name] = Object.defineProperty({}, 'prop', {
enumerable: true,
configurable: true,
get: () => this.prop,
set: (v) => {
Object.defineProperty(item, 'prop', {
enumerable: true,
writable: true,
value: v
});
}
});
}
}
let attempt = new Filters();
attempt.add('first');
attempt.add('second');
attempt.list['second'].prop = 'custom';
console.log(attempt.list['first'].prop, attempt.list['second'].prop);
// → foo custom
attempt.prop = 'bar';
console.log(attempt.list['first'].prop, attempt.list['second'].prop);
// → bar custom