Kак сделать чтобы при изменении одного значения объекта менялось зависящее от него

Есть некоторый объект Graph. У него есть свойство width и зависящее от него height. Как сделать, чтобы при изменении значения width вместе с ним менялось и height?

function Graph(width){
this.width=width
this.height=0.5*this.width
}
Graph.prototype.changeWidth=function(){
this.width*=2
}

let graph=new Graph(10)
graph.changeWidh()
console.log(graph)
//→Graph{width:20, height:5}
/*Должно быть: 
    Graph{width:20, height:10}*/

Ответы (1 шт):

Автор решения: Алексей Шиманский

function Graph(width){
  this.width = width;
  this.changeHeight();
}

Graph.prototype.changeHeight = function(){
    this.height = 0.5 * this.width;
}

Graph.prototype.changeWidth=function(){
    this.width *= 2;
    this.changeHeight();
}

let graph=new Graph(10);
console.log(graph);
graph.changeWidth();
console.log(graph)

→ Ссылка