8.15 - The This Keyword! (Yes ... another one!)


#1

Based on what I’ve read here:





It seems that the same question keeps coming up … and these are not all of them! This is just from the first page of results!

8.10 - The This Keyword I Instruction 1 asks us to:
"Let’s make the .sayHello() method a bit more interesting. Use this to return a string that says:
Hello, my name is ${name here}
In place of the name here, use this to interpolate your name property."

So I modified my existing sayHello() function to contain the following:
return 'Hello, my name is ${this.name}';
which resulted in the output:

Hello, my name is ${this.name}

It seems the string interpolation is not accepted.

I noticed that my sayHello() function was using arrow syntax from the previous lesson, 8.8 Methods example and Instruction 1.
However, changing this to the new Es6 method:

  sayHello() {
    return 'Hello, my name is ${this.name}';
  },

or the traditional method:

  sayHello: function() {
    return 'Hello, my name is ${this.name}';
  },

still results in the same output:

Hello, my name is ${this.name}

Now, the instructions in lesson, 8.10 - The This Keyword I do not specify that I should encounter this output. Nor does the lesson hint mention anything on how to correct it:

You can connect this to a property within the object to use that property inside of a method. this represents the person object in this case, therefore writing this.name will equal the name key’s value.
Use the following syntax to interpolate the name key into the string. `This is how you interpolate a variable like ${myVariable}`

Which is exactly what I’ve done!!!
The lesson clearly does not expect this output I receive (failure of the string interpolation) as Instruction 2 specifies:

  1. Notice that the output in the console now displays your name. We just wrote a function that uses a property in its enclosing scope, thanks to the this keyword.

The ONLY way I was able to progress to Instruction 2 was to not use string interpolation at all:

  sayHello: function() {
    return 'Hello, my name is ' + this.name;
  },

resulting in the output:

Hello, my name is jimjamz

Clearly, something is wrong with the mechanism of string interpolation in the main.js. As, the instructions (nor the hint) are clearly expecting this to happen. With the amount of questions based on the same exact problem too, then this is a clear pointer to something unexpected occurring in this tutorial.


#2

i am unsure how the question is related to this keyword, seems your question focuses on es6 string interpolation, in order for es6 string interpolation to work, the string should enclosed in backticks (grave accent) not apostrophe.


#3

I’m not sure I fully understand your response but this question was raised in relation to the chapter and instruction number on where the error is occurring within the Learn JavaScript tutorial.

https://www.codecademy.com/courses/learn-javascript-objects/lessons/objects/exercises/this


#4

yes, there is. You used the wrong syntax.

But everything within the question, focuses on string interpolation. I don’t see anywhere you have a question related to this keyword, and i answered what you did wrong:


#5

Please indicate to me exactly where in the instructions does it specify to use backticks, other than the code example shown.
As this is a tutorial for beginner coders, it probably would have been more useful for the instructions to highlight this (maybe use the word backtick in the instructions or hint examples?)
as opposed to leaving it to the student to discern between ` and '.
Additionally, it would have been less confusing for the lesson to use an example of a return statement that didn’t use both:

 openRestaurant: function() {
    if (this.hasDineInSpecial) {
      return 'Unlock the door, post the special on the board, then flip the open sign.'

#6

Hi @jim.jamz,

While I can understand your point of view, being a beginner, one might need everything spelled out clearly for the instructions. It would be much easier to follow and sprint through the lessons. However, that would also mean that users become too dependent on the instructions during the learning process and might be growing to rely on more information. As the habit sets in, once the information is getting lesser and lesser, the feeling of lost is inevitable.

I feel your frustration, but do know that those who offer to help in this forum are not at all involved in the curriculum/lessons creations. So, asking one who helped, to point out something to prove your point is a bit fruitless and pointless, as we all are just, CC users, some stay a little longer, some decide to help others. That said, we do have a section in the community forum:


You may direct your suggestions there and hopefully the engineers will have a look on your suggestion and take into account of your opinion.

@stetim94 was right about pointing out the wrong string interpolation syntax. As the lesson is about this (you’re correct about this), yet the main error is about the wrong interpolation syntax, if you follow the correct syntax using backtick `, it would not be an issue. :slight_smile:

One thing should be commended is that you took the time to read some previous posted topics, go through them and have a good grasp of formatting your post accordingly to ease the reading. For that, thank you.

And lastly, for your question, yes, it might not be very clear at the point where you’re at now, as it is around the middle part of the JavaScript track. The author would assumed you have already understood the basics of different syntax. Remember, you did learn it in the beginning of the JavaScript track. Here:
https://www.codecademy.com/courses/learn-javascript-introduction/lessons/variables/exercises/string-interpolation-ii

String Interpolation II

In the newest version of JavaScript (ES6) we can insert variables into strings with ease, by doing two things:

  1. Instead of using quotes around the string, use backticks (this key is usually located on the top of your keyboard, left of the 1 key).
  2. Wrap your variable with ${myVariable}, followed by a sentence. No +s necessary.

All that is just my humble opinion and you’re surely entitle to your own opinion as well. That said, happy coding! Cheers :slight_smile:


#7

@codexthon is right, the less we help, the more you figure out on your own, the better

I perfectly know the answer to your problem. But if i just tell you, you learn very little. Yes, if i point out the error and give you the correction, it might feel like you learned it, but that isn’t actual the case

Its your task to break my answer down, what is string interpolation? Where do i use it? What does enclosing string mean?

Programming is a way of thinking, in fact, most programming involves problem solving and relatively little coding. The less depended you become on fixed instructions, the better. This applies from the start.

Its a mindset, which i was trying to help you create. From my side, don’t expect full on answers, there value is seriously overrated


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.