**Mysterious Organisms**

Hello, my problem is that the object will not log the parameter values when I input them and then call the factory function I have created in the project.

// Returns a random DNA base
const returnRandBase = () => {
  const dnaBases = ['A', 'T', 'C', 'G']
  return dnaBases[Math.floor(Math.random() * 4)] 
}

// Returns a random single stand of DNA containing 15 bases
const mockUpStrand = () => {
  const newStrand = []
  for (let i = 0; i < 15; i++) {
    newStrand.push(returnRandBase())
  }
  return newStrand
}

const pAequorFactory = (specimenNum, array) => {
  return {
    specimenNum: specimenNum,
    get specimenNum () {
      return this.specimenNum
    },
    set specimenNum (specimenNumIn) {
      this.specimenNum = specimenNumIn
    },
    dna: array,
    get dna () {
      return this.dna
    },
    set dna (dnaIn) {
      this.dna = dnaIn
    },
  }
};

console.log(pAequorFactory(3, mockUpStrand()));

It does log the keys and Getter/Setter. But not the value for the keys which I have inputted in the function call

I think it has something to do with naming. Your getters are the same name as your property:

    specimenNum: specimenNum,
    get specimenNum () {
      return this.specimenNum
    },

this.specimenNum will prioritize the getter over the property (at least that is my understanding), and that is why you are seeing the functions print instead of your property values.

There is an issue here though - since your getter is the same name as the property, calling the getter will result in a stackoverflow error. When this.specimenNum is called in the getter, it sees that as calling the getter and it will call itself repeatedly.

Getters and setters are only necessary if you want to signal to other programmers that the property should not be directly altered. That signal is the underscore _ and should be used everywhere you want to refer directly to the property.

You have a choice to make here, add the underscore to the property and all direct references to it, or remove the get / set functions.

    _specimenNum: specimenNum,
    get specimenNum () {
      return this._specimenNum
    },
3 Likes

Thank you that worked out

1 Like