0

I am creating an object in JavaScript that holds employee information (name, rate, hours, gross pay). In order to calculate the gross, I need to multiply the hours times the rate. My teacher says I should be able to do it right inside my object, but it is not working. Here is my code:

const emp1 = {
  name: "John Doe",
  rate: 13.25,
  hours: 20.25,
  gross: rate * hours
};
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
  • Could you show us what you have try in a minimal reproductible sample ? – jeremy-denis Mar 11 '22 at 16:17
  • https://stackoverflow.com/questions/4616202/self-references-in-object-literals-initializers – cmgchess Mar 11 '22 at 16:17
  • 1
    OP is saying he wants to define `gross` from properties mentioned while defining the object. I doubt his teacher meant in the way OP shows. `this` can't work because the object is not completely defined until it is. ... Instead, leave out `gross:` or set to null. Then use `emp1.gross = emp1.rate * emp1.hours;`. – GetSet Mar 11 '22 at 16:20
  • Another way was to use the [`get` syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) ... `const emp1 = { name: "John Doe", rate: 13.25, hours: 20.25, get gross () { return this.rate * this.hours; } }; console.log(emp1.gross);` – Peter Seliger Mar 11 '22 at 16:30

4 Answers4

2

In javascript you can define functions (methods) on an object and call them as you need :)

Note the method syntax, and the use of 'this' to refer to properties on the object itself.

var emp1 = {
        name: "John Doe",
        rate: 13.25,
        hours: 20.25,
        gross: function () {
          return this.rate*this.hours
        }
    };

console.log(emp1.gross())
jpmc
  • 1,147
  • 7
  • 18
1

Another way was to use the get syntax ...

const emp1 = {
  name: "John Doe",
  rate: 13.25,
  hours: 20.25,
  get gross () { return this.rate * this.hours; },
};
console.log('emp1.gross ...', emp1.gross);

Thus the idea of cause is the same like what James McGlone did suggest, one just is free of omitting the call operator when accessing the value of this (computed) property.

Both solutions are equally valid, one just does not know which one will please the teacher.

Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
0

You need to declare the variables outside the object first

var rates = 13.25
var hours = 20.25

var emp1 = {
    name : "John Doe",
    rates,
    hours,
    gross: rates * hours
}
BigL
  • 165
  • 1
  • 2
  • 15
  • ... nope, neither you nor the OP wants this solution for when either of the `emp1` property values ... `emp1. rates` / `emp1. hours` ... gets changed `emp1. gross` is not going to reflect this change. – Peter Seliger Mar 11 '22 at 16:48
0

Here, gross is a function of rate and hours. So, I should see that in your code.

So, you can write:

var emp1 = { 
  name: "John Doe", 
  rate: 13.25, 
  hours: 20.25, 
  gross: function () {
    return this.rate * this.hours;
  }
};

And you can get your gross value this way:

emp1.gross();

N.B:

Just pay attention to var and this in your code. I'm here to help and elaborate more.

Abder
  • 81
  • 2