Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
Cómo establecer nuevos valores para una clase

Tengo una clase de rectángulo y quiero usar shift para asignar nuevos valores a this.x y this.y por la cantidad de cambio. Por ejemplo, las coordenadas que se le asignaron (5,5) r.shift(3,3) harán que this.x y this.y (3,3). Actualmente, mi código crea valores nuevos para x e y, no los reasigna. ¿Cómo continuaría para hacerlo?

 class Rectangle { constructor(x, y, width, height){ this.x = x; this.y = y; this.width = width; this.height = height; } } Rectangle.prototype.shift = function (changeInX, changeInY) { this.x = changeInX this.y = changeInY } //returns value string Rectangle.prototype.toString = function () { return 'x is ' + this.x + ', y is ' + this.y + ', width is ' + this.width + ', height is ' + this.height } //offsets coordinates by amount Rectangle.prototype.offset = function (changeInX, changeInY) { return new Rectangle(this.x+changeInX, this.y+changeInY, this.width, this.height) }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Debe usar += en lugar de solo = para incrementar this.x en su función de shift .

p.ej:

 this.x += changeInX this.y += changeInY

Ejemplo modificado completo:

 class Rectangle { constructor(x, y, width, height){ this.x = x; this.y = y; this.width = width; this.height = height; } } Rectangle.prototype.shift = function (changeInX, changeInY) { this.x += changeInX this.y += changeInY } //returns value string Rectangle.prototype.toString = function () { return 'x is ' + this.x + ', y is ' + this.y + ', width is ' + this.width + ', height is ' + this.height } //offsets coordinates by amount Rectangle.prototype.offset = function (changeInX, changeInY) { return new Rectangle(this.x+changeInX, this.y+changeInY, this.width, this.height) } const rect = new Rectangle(1, 2, 3, 4); console.log('before:', rect.toString()) rect.shift(100, 200); console.log('after:', rect.toString())

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda