JavaScript Practice Object Basics in JavaScript Practicing Object Basics Adding a Method Solution

Why can't you use this.string and instead you're using myString.string?

Why can't you use this.string and instead you're using myString.string?

3 Answers

It might help if you post some code for context. Without seeing that code, I would suggest you think about what "this" is referring to at any point. It's a subject with some depth, but the basic jargon is that it is determined by the invocation setting. I haven't looked at the code or the challenge yet, but I suspect "this" is being avoided because it would refer to something happening in a parent function or a prototype, rather than the instance "myString" represents.

Bader Alsabah
Bader Alsabah
4,734 Points

I think Barry above hit the spot with the answer - but just to perhaps add some clarity, as I have been following the video of this practice - the instructor does use the "this" keyword when referencing a property within the object declaration. In that context - "this" refers to the object and therefore using .dot notation on the object is valid. However, when she posts or console.logs the array - "this" wouldn't be referring to "myString" at that point or in that context. In fact if you try console.log with keyword "this" you will see that the value is undefined.

Steven Vallarsa
Steven Vallarsa
10,841 Points

I got

countWords() {return this.string.split(" ").length}

to work.