Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

406
Vistas
How to use javascript object function to return a value and use it as object property.?

In selling price I expect to get 1000-20% i.e. 800 by passing MRP and discount as arguments to the function get_selling_price, but my code gives error get_selling_price undefined get_selling_price. Please help me to fix it and let me know why this happened, since I declared the function before using it.

let Sock = {
    brand: 'JS',
    color: 'Red',
    size: "extra-extra small",
    MRP: 1000,
    discount: 20,
    
    get_selling_price: (MRP, discount)=>{return MRP-((MRP*discount)/100)},
    selling_price: get_selling_price(this.MRP, this.discount),

    buy: ()=>console.log(`You are buying ${this.brand}`),
    sell: function(){console.log(`You are selling ${this.brand}`)},

}

console.log(Sock);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In JavaScript, if you want to access the information stored inside the object from the object method, you need to use regular function() instead of arrow function. In your case, to access the get_selling_price function inside selling_price of the Sock object, you need to define selling_price as a regular function and access another function/property of the object using this keyword (see sample below).

let Sock = {
  brand: "JS",
  color: "Red",
  size: "extra-extra small",
  MRP: 1000,
  discount: 20,

  get_selling_price: (MRP, discount) => {
    return MRP - (MRP * discount) / 100;
  },
  selling_price: function () {
    return this.get_selling_price(this.MRP, this.discount);
  },

  buy: () => console.log(`You are buying ${this.brand}`),
  sell: function () {
    console.log(`You are selling ${this.brand}`);
  },
};

Also, just to mention, I recommend you get acknowledged with this keyword in JavaScript, you can refer to this documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda