Scoping Question

Hello, this is what I am instructed to do:

Your research team wants to be able to compare the DNA sequences of different P. aequor. You’ll have to add a new method (.compareDNA()) to the returned object of the factory function.

.compareDNA() has one parameter, another pAequor object.

This is my constructor object thus far:

const pAequorFactory = (specimenNum, dna) => { return { specimenNum: specimenNum, dna: dna, mutate: mutate(dna), compare: compare() } }

How can I get another object into my object in the factory constructor like I did for mutate(dna)?

Please post a link to the exercise where this code is explored.

Thank you for your response!

It is one of the projects after the Javascript part II course:

https://www.codecademy.com/journeys/full-stack-engineer/paths/fscj-22-building-interactive-websites/tracks/fscp-22-javascript-syntax-part-ii-c8ddbace-1463-4797-ae12-503c7b0f9552/modules/wdcp-22-mysterious-organism-8119c94c-f8eb-4254-8520-dfe4afc233a1/projects/mysterious-organism

From the little bit of code you have provided it is difficult to tell what you have planned. Have you learned about methods and the this variable?

1 Like

So you just couldn’t contain yourself and wait for the member to answer our simple question before hijacking the topic? Good for you. Take over. What does it matter if the member doesn’t have the requisite knowledge?

1 Like

Ah I think this is what I may be looking for (I will need to try when I get home from work), but essentially I wanted to be able to write a function that takes another object and have the function be a property of the pAequorFactory object. When I tried to define a function with a parameter like:

compare (otherOrganism) {
//code
}

I would get an error, because ‘otherOrganism’ was not a variable in the object like ‘dna’ is for ‘mutate(dna)’

Is that making sense?

@mtf, thank you for your response! I did complete the unit on ‘this’ and my notion of it is as somehow it represents the calling object, so it’s like a stand in for the object when you are working within the definition of the object’s properties. Perhaps I can use ‘this’ to differentiate between the calling object and the ‘otherObject’ that I want to use as a parameter.

Ask me in a day. The other member is more interested in giving you code than in determining where you are in the learning curve, which is what interests me. Ping me tomorrow if you have still not got your bearings.

I will give you a hint: both inputs are factory returned objects. Keep that in mind, as well as looking more closely at this and how it plays a role.

1 Like

Hello @mtf,

I got a chance to return to my regular study schedule today. I reviewed the lesson on advanced objects and ‘this’. I think I am on the way to completing the project. It’s the “Mysterious Organism” project in the Javascript II curriculum.

I’m experimenting with the use of this and made a function within an object that exhibits interesting and, I think, illuminating behavior:

const pAequorFactory = (specimenNum, dna) => {
  return {
    specimenNum: specimenNum,
    dna: dna,
    mutate: function (dna) {
      let mutateNum = getRandomBaseNum();
      let baseCharacters = ['A','C','G','T'];
      let mutationOptions = baseCharacters.filter(e => e !== dna[mutateNum]);
      let mutateChar = selectRandomElement(mutationOptions);
      // console.log(mutateNum)
      // console.log(dna)
      // console.log(mutateChar)
      dna[mutateNum] = mutateChar;
      // console.log(dna)
      return dna;
    },
    compare: function (dna) {
      console.log(`I'm ${this.specimenNum} and my DNA is ${dna}`)
      console.log(`I'm ${this.specimenNum} and my DNA is ${this.dna}`)
    }
    
  }
}

console.log(specimen2.compare(specimen1.dna))

As I rubber duck this I’ll set up the following situation: there are two objects of the same (class?)… there are two objects:
specimen1 and specimen2.

In the last function, “compare”, I see that the first line is referencing the dna of the object passed in as an argument. This will be specimen1’s dna. In the second line the the dna of specimen2 is reference via the this code.

I think this will allow me to do what I want which is compare two sequences of dna. I’m sure I’ll have some bumps along the way. Thanks for redirecting me to the right lesson.

1 Like

Good to see you haven’t walked away from this problem, and keep at it!

Let’s rework your call to make ‘specimen1’ the this object, and (here’s the secret) ‘specimen2’ the that object. The parameter will not be dna but the second specimen.

specimen1.compare(specimen2)
compare: function (dna) {
      console.log(`I'm ${this.specimenNum} and my DNA is ${dna}`)
      console.log(`I'm ${this.specimenNum} and my DNA is ${this.dna}`)
}

would become,

compare: function (that) {
      console.log(`I'm ${this.specimenNum} and my DNA is ${this.dna}`)
      console.log(`I'm ${that.specimenNum} and my DNA is ${that.dna}`)
}

I’m not meaning to give away the solution, but the above should be revealing as to how we work with two instances in a single method. You should still have a question or two after looking at and trying this out. Ask away, as needs be.

Aside

It will save you a lot of work if you adopt the new method syntax:

compare (that) {

}

Further Aside

It follows that since both instances possess the same properties and methods, it matters not whether we switch which instance is this and which is that. We should keep in mind, though, that this is a defined property with direct reference to the owner object; i. e., the execution context.

Extrapolating this further, we see that is also its own execution context.

This concept relates to the subject, ‘encapsulation’ to some degree. We have some closure around object instances.

1 Like

Very cool! I see now that my initial problem was that my syntax for defining functions in objects was wrong (not the comapre: function {} … but leaving the code block {} out within the object… in essence I was making the function outside the object and attempting to call it inside the object) I’ll try to get used to the new syntax you mentioned. Thanks!

1 Like