Почему в этом примере принуждение не удается ++?Javascript

Форум по Javascript
Ответить
Anonymous
 Почему в этом примере принуждение не удается ++?

Сообщение Anonymous »


Код: Выделить всё

class Variable {
#value;
#dependents = new Set();
constructor(value) {
this.value = value;
}
get value() { return this.#value; }
valueOf() { return this.value; }
toString() { return String(this.value); }
toNumber() { return Number(this.value); }
set value(value) {
if (this.#value === value)
return;
this.#value = value;
var change = new WeakSet();
for (const dep of this.#dependents)
dep.update(change);
}
register(dependent) {
if (!(dependent instanceof Dependent))
throw new TypeError("Not a Dependent");
this.#dependents.add(dependent);
}
}

class Dependent extends Variable {
constructor(variables, calculation) {
super();
this.variables = variables;
this.calculation = calculation;
for (const variable of variables) {
if (!(variable instanceof Variable))
throw new TypeError("Not a Variable");
variable.register(this);
}
this.calculate();
}
update(change) {
if (change.has(this))
throw new Error("Cyclic dependency");
change.add(this);
this.calculate();
}
calculate() {
this.value = this.calculation(...this.variables);
}
}

var a = new Variable(1);
var b = new Variable(1);
var c = new Dependent([a, b], (a, b) => a + b);
console.log(`${a} + ${b} = ${c}`);
a.value++;
console.log(`${a} + ${b} = ${c}`);
a++;
console.log(`${a} + ${b} = ${c}`);


Принуждение работает для A+B , но не удается для ++ < /код>. Почему?

Подробнее здесь: https://stackoverflow.com/questions/794 ... is-example
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»