0

Just started learning JS and I was hoping someone could explain how I can use the global function add2 to fill in the combined AgesPlus2?

function add2(num1, num2) {
  return (num1 + num2) + 2;
};

let people = {
  age1: 43,
  age2: 23,
  combinedAgesPlus2: add2(age1, age2)
}
AmmoPT
  • 958
  • 1
  • 9
  • 16

4 Answers4

3

The function call is fine, its just that the object only exists after it was declared and not inbetween, so you cannot access age1 inside the object literal. You could set it afterwards:

 let people = { 
  age1: 43,
  age2: 23,
 };

 people.combinedAgesPlus2 = add2(people.age1, people.age2);

Read on here:

Self-references in object literal declarations

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Thanks! That was super helpful, I was able to get it and understand the calls better! ` function add2(num1, num2) { return (num1 + num2) + 2; } let people = { age1: 43, age2: 23, get combinedAges() { return add2(this.age1, this.age2); } } console.log(people.combinedAges) ` – Bradley Kiser Aug 21 '18 at 17:20
1

You could use a getter:

function add2(num1, num2) {
  return (num1 + num2) + 2;
};

let people = {
  age1: 43,
  age2: 23,
  get combinedAgesPlus2() {
    return add2(this.age1, this.age2);
  }
}

console.log(people.combinedAgesPlus2);

As paulpro mentions in the comment below, this would calculate the value each time you access the property, rather than upon instantiation.

The good thing about this is that your value will always be current, despite updates to the properties it's referencing:

function add2(num1, num2) {
  return (num1 + num2) + 2;
};

let people = {
  age1: 43,
  age2: 23,
  get combinedAgesPlus2() {
    return add2(this.age1, this.age2);
  }
}

//DEMO
console.log("combinedAgesPlus2: ", people.combinedAgesPlus2);

people.age1 = 10;
console.log("age1 changed to 10");

console.log("combinedAgesPlus2: ", people.combinedAgesPlus2);
Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
  • This doesn't evaluate while the object is being created, but instead every time the property is accessed. If `people.age1` is modified later then the next time `people.combinedAgesPlus2` is accessed it will also be different. The OP may prefer it this way, but the differences should probably be highlighted in the answer. – Paul Aug 21 '18 at 17:20
0

You can't access properties of an object literal before the object is finished being created. If you want to read and write properties during object creation you need to use a constructor function instead of an object literal:

function add2( num1, num2 ) {
  return num1 + num2 + 2;
}
 
let people = new function ( ) {
  this.age1 = 43;
  this.age2 = 23;
  this.combinedAgesPlus2 = add2( this.age1, this.age2 );
};

console.log( people );
Paul
  • 139,544
  • 27
  • 275
  • 264
0

You can use getters for that:

function add2(num1, num2) {
  return (num1 + num2) + 2;
};

let people = {
  age1: 43,
  age2: 23,
  get combinedAgesPlus2() { return add2(this.age1, this.age2) } 
}

console.log(people.combinedAgesPlus2)
ymd
  • 682
  • 5
  • 18