Using 'this' and factory methods- am confused

So, in the ‘Mysterious Organism’ project, I’ve been trying to get the hang of advanced objects.
I created a factory function for the organism, and am in the middle of adding the object methods, but it seems that I cant really use ‘this’ in factory methods? it tells me: “TypeError: Cannot read property ‘length’ of undefined
at Object.compareDNA”

but this is a function that compares between two objects but only receives one as an argument. It was natural for me to use ‘this’, and I cant understand why it doesnt work. What should I use if not ‘this’?

I’m confused.


Could you paste your formatted code here? It’ll assist us in reviewing your issue.

TypeError: Cannot read property ‘length’ of undefined
doesn’t read as an issue with this, but rather an issue with what you’re referencing. Are you sure either this object, and the object you’re passing to the compareDNA method both exist (and are not undefined)?

The other possible issue: You are trying to reference a variable object, but accidentally typed Object, to reference the Object class. Make sure you’re typing the variable name correctly.

Without your code, it’s hard to tell.

well, I eventually got it to work using antoher method, but I’ll show you what I’ve been trying to do:
Look at the compareDNA method, where I’ve been trying to use ‘this’

// 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++) {
  return newStrand

//specimen constructor
const pAequorFactory = (serial,dnaBase)=>{
  return {specimenNum: serial,
          dna:  dnaBase,
          //change one base of dna
          mutate: () =>{
            const randIndex= Math.floor(Math.random() * 15);
            let newBase = returnRandBase();
            while(dnaBase[randIndex] == newBase){
              newBase = returnRandBase();
          //conparing DNA of two speciments
          compareDNA: (otherCell)=>{
            let i, similar=0,precentage;
            for(i=0; i<this.dna.length; i++){
              if(this.dna[i] == otherCell.dna[i] ){
            precentage= 100*(similar/this.dna.length);
            console.log(`specimen ${this.specimenNum} and specimen ${otherCell.specimenNum} have ${precentage}% DNA in common`);

const cell= pAequorFactory(1,mockUpStrand());
const cell2= pAequorFactory(2,mockUpStrand());