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

408
Visualizações
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 Respostas
Responde à pergunta

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 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