TransWikia.com

How do I set a value as a parameter inside an object with plain javascript?

Stack Overflow Asked by bob saggy on December 22, 2021

Consider

a = {apple: "the value of this is ${value}"}

I want to call on this such that

a = {apple: "the value of this is ${value}"}
console.log(a.apple("16")) => outputs "the value of this is 16"

Anyway to do this in javascript?

2 Answers

Maybe a setter

const a = {
  _apple: "the value of this is ",
  set apple(value){ 
      this._apple = this._apple + value;
    }
};

a.apple = '16';

console.log(a._apple);   

Answered by sonEtLumiere on December 22, 2021

do you mean as following:

const a = { apple: (value) => `the value of this is ${value}` }

Answered by tmhao2005 on December 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP