0

How can I call properties within the datasets object itself, in this case to create fullName with already existing data?

calling this. shouldn't work? When i'm try occurs > "Uncaught TypeError: Cannot read properties of undefined (reading 'firstName')"

Script:

var datasets = {

  cst_TEST: {
    firstName: "John",
    lastName: "Deacon"
  },

  glb_TEST: { age: "30" },

  fullName: this.cst_TEST.firstName + " " + this.cst_TEST.lastName

}

var kOBJ = {

  fxAAA(){ console.log("First Name: " + datasets.cst_TEST.firstName); },

  fxBBB(){ console.log("Last Name: " + datasets.cst_TEST.lastName); },

  fxCCC(){ console.log("Age: " + datasets.glb_TEST.age); },

  fxDDD(){ console.log("Full Name: " + datasets.fullName); }

}

Output:

<script>

  kOBJ.fxAAA(); //output: `First Name: John`

  kOBJ.fxBBB(); //output: `Last Name: Deacon`

  kOBJ.fxCCC(); //output: `Age: 30`

  kOBJ.fxDDD(); //output (should be): `Full Name: John Deacon` - err: Uncaught TypeError: Cannot read properties of undefined (reading 'firstName')

</script>
Edgaras
  • 404
  • 2
  • 16
  • 3
    Make `fullName` a function and call it, or use a [getter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get). – evolutionxbox Jun 28 '23 at 12:59
  • ty , works: `get fullName(){ return this.cst_TEST.firstName + " " + this.cst_TEST.lastName; }` – Edgaras Jun 28 '23 at 13:15

0 Answers0