1

here is my code:

let font = {
  info: {
    height: 5,
    width: 5,
    margin: 1,
  },
  A: [" ### ", "#   #", "#####", "#   #", "#   #"],
  a: [" ### ", "#   #", "#####", "#   #", "#   #"],
  B: ["#### ", "#   #", "#### ", "#   #", "#### "],
  b: ["#### ", "#   #", "#### ", "#   #", "#### "],
  C: [" ####", "#    ", "#    ", "#    ", " ####"],
  c: [" ####", "#    ", "#    ", "#    ", " ####"],
};

I want to set the value of a to A, how do I do that? I've tried a: this.A, a: font.A and a: ( function(){ return this.A } )(), but no one worked

1 Answers1

3

You can use a getter:

const font = {
  info: {
    height: 5,
    width: 5,
    margin: 1,
  },
  A: [" ### ", "#   #", "#####", "#   #", "#   #"],
  get a() { return this.A; },
  B: ["#### ", "#   #", "#### ", "#   #", "#### "],
  b: ["#### ", "#   #", "#### ", "#   #", "#### "],
  C: [" ####", "#    ", "#    ", "#    ", " ####"],
  c: [" ####", "#    ", "#    ", "#    ", " ####"],
};

console.log(font.a);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
  • Please vote to close duplicates. You have answered a similar question yesterday. https://stackoverflow.com/a/68145023' – adiga Jun 28 '21 at 08:16
  • is it possible to use arrow functions? –  Jun 28 '21 at 08:16
  • @Khalidshammout you can't use arrow functions: [ES6 getter/setter with arrow function](https://stackoverflow.com/questions/33827519) – adiga Jun 28 '21 at 08:18