2

He visto varios ejemplos en los que devuelve directamente el resultado y en otros retornan de esta manera this.nueva_variable. En el ejemplo de abajo la variable bmi no se ha creado en el objeto.

      calcBMI: function () {
        return this.mass / this.height ** 2;
      },

o

      calcBMI: function () {
        this.bmi = this.mass / this.height ** 2;
        return this.bmi;
      },
3
  • Estás en Stack Overflow en Español, por favor, traduce tu pregunta el 28 feb. 2021 a las 21:54
  • La pregunta está escrita en un idioma diferente al idioma oficial de este sitio, que es el español. el 28 feb. 2021 a las 21:56
  • Las dos maneras de devolver el valor funcionan aparentemente igual en términos de devolución pero el segundo (usando this.atributo) asigna el valor a un miembro del objeto en uso y lo deja "disponible" dentro del mismo para usos futuros.
    – quevedo
    el 28 feb. 2021 a las 22:39

1 respuesta 1

6

Revisé el caso, les comparto mis notas.

Memory Allocation registra variables durante el levantamiento, pero no propiedades. Ahora, propiedades como this.bmi, la cual no forma parte de las propiedades del objeto _Objeto, se registran en memoria con valor tipo undefined:

let _Objeto = {
  mass:30,
  height:2,
  keepTrackOf: function(){
    console.log("keepTrackOf this.bmi: ", this.bmi);
    
  },
  calcBMI01: function () {
    return this.mass / this.height ** 2;
  },
  calcBMI02: function () {
    this.bmi = this.mass / this.height ** 2;
    return this.bmi;
  }
}

let instance = {..._Objeto};
instance.keepTrackOf();

Por otro lado, si se solicitan las propiedades del Objeto, this.bmi no se devuelve:

let _Objeto = {
  mass:30,
  height:2,
  keepTrackOf: function(){
    console.log("keepTrackOf this.bmi: ", this.bmi);
    
  },
  calcBMI01: function () {
    return this.mass / this.height ** 2;
  },
  calcBMI02: function () {
    this.bmi = this.mass / this.height ** 2;
    return this.bmi;
  }
}

let instance = {..._Objeto};
instance.keepTrackOf();
console.log("Propiedades de instance: ", Object.keys(instance));

Una vez ejecutádo el método que usa a this.bmi, la misma ya se devuelve como otra propiedad más del Objeto:

let _Objeto = {
  mass:30,
  height:2,
  keepTrackOf: function(){
    console.log("keepTrackOf this.bmi: ", this.bmi);
    
  },
  calcBMI01: function () {
    return this.mass / this.height ** 2;
  },
  calcBMI02: function () {
    this.bmi = this.mass / this.height ** 2;
    return this.bmi;
  }
}

let instance = {..._Objeto};
console.log("Propiedades de Objeto: ", Object.keys(instance));
instance.keepTrackOf();
console.log("calcBMI02: ",instance.calcBMI02());
console.log("despues de usar calcBMI02: ", Object.keys(instance));
instance.keepTrackOf();

Después de observar este comportamiento, considero que si se va a usar esta propiedad this.bmi de forma regular por el programa, lo más correcto sería incluirla en las propiedades del objeto explícitamente, para evitar confusiones sobre lo que hace y cómo se usa. Así lograremos que nuestro código sea más inteligible.

Espero que esta respuesta sea de ayuda.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.