The properties we have been working with so far are known as data properties. These are the properties where we give them a name and assign a value to them:
Thanks! You can credit @senocular for making me write it as part of addressing feedback on the upcoming edition of the JavaScript: A Beginnerâs Guide
Hi There. I have some questions about the use of underlines in this Getters and Setters tutorial. Iâve Googled around to understand why one would use a _ at the beginning of a variable in JS and I find chatter about the underline being an indicator of a private variable. I think in this instance you are using it for a different reason maybe? Using the Shout Generator section as a basis for these questions, I see you create a _message property and create a setter named âset messageâ. If I throw out putting a _ in the message property, then the this.message = value.toUpperCase(); creates strange behavior (like recursive). If I leave it in, I see later when I issue shout.message = âThis is sparta!â; that the setter is called. So, we are somewhat hiding the implementation of the setter, right? We are not directly setting _message, but secretly calling the setter? Also, the setter doesnât necessarily have to have the same name as the property it sets as in the zorb example, right? Iâm trying to understand the sudden appearance of the underline before the property name in this section.
Iâve Googled around to understand why one would use a _ at the beginning of a variable in JS and I find chatter about the underline being an indicator of a private variable. I think in this instance you are using it for a different reason maybe?
Itâs related to the explanations youâve found. Kirupaâs creating some backing storage for the getters and setters, but itâs an implementation detail, so it doesnât need to be public.
Something subtle between the zorb example and the shout example is that in the zorb example, the backing property is named message, but the getters and setters are called greeting. By contrast, in the shout example, the backing property is _message and the getters and setters are message. When you take out the leading _, youâre creating a naming conflict so that the set message setter calls itself in its own definition, leading to the recursive behavior you described.
The part thatâs hidden is the name (and storage) of the _message property. The public interface is the accessors (getters and setters) that donât start with _. So youâre not hiding the fact that youâre calling the setter, youâre hiding what the setter is doing in its implementation.
Right; itâs almost always going to be a bug if it does.
I think the main problem is that Kirupa swapped from message+greeting to _message+message without explaining the rationale.
thank you so much for all your effort and help with the Tutorials.
I just want to say, that your YouTube video is not the correct on the JavaScript 101, getter and setter Getters and Setters!
It should be the Getters and Setters in JavaScript - YouTube
Best regards,
Marco
Hi @Marco_L - glad you liked the content! The links you shared seem to be the same. One is the video itself and the other is the article with the video embedded. Am I missing an important detail?
Hi Kirupa, maybe it was due to a cookie or something similar, but just to be completely sure, I tried using another browser, and the Youtube video in the Getter and Setter article did not display the correct video. Itâs likely a bug on my end!
By the way, the visuals and explanation are really great!
1 Like
Creating engaging and entertaining content for designers and developers since 1998.