How can we deare a variable of a class as protected in JavaScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can we deare a variable of a class as protected in JavaScript?

21st May 2021, 9:18 AM
Aakash Shivanshu
Aakash Shivanshu - avatar
3 Answers
+ 2
By prefixing names by # you can declare them private. I don't know if there is any active proposal for protected field right now. This feature request is still at stage 3 and browser support is limited. You shouldn't use it in code unless it reaches stage 4 and major browsers finish implementing it. There is a browser compatibility table and some explaination here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields More on standardization process: https://sergioviniciuss.medium.com/ecmascript-standardization-process-94a64acf771f https://tc39.es/
21st May 2021, 9:32 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
Thanks omkar
21st May 2021, 10:03 AM
Aakash Shivanshu
Aakash Shivanshu - avatar
0
I guess using underscore in front of variable is what javascript is planning. Like: let _name:"sololearn"
21st May 2021, 6:02 PM
Aakash Shivanshu
Aakash Shivanshu - avatar