Рет қаралды 7,318
Have you noticed a hash symbol showing up in Angular code samples? If not, you may see it soon! What is the purpose of the # and when should you use it?
The # symbol was recently added to JavaScript to denote private class properties. Making a class variable private means that the variable can only be accessed within its class. That allows us to encapsulate data we only want to access within a service.
But don't we already have a private accessor for our class fields? Yep!
Then why do we need the new hash syntax?
Let's take a look at the private accessor first, then examine the # syntax and why it is a better choice in our Angular applications.
*Content*
00:00 JavaScript private class members (#)
00:50 The sample application
01:16 The danger of public class properties
02:47 TypeScript's private accessibility feature
03:39 Accessing variables with private accessibility at runtime
05:41 JavaScript's private class members (#)
07:11 Private properties in components
08:04 Wrap Up
*Links*
TypeScript documentation on private and #: www.typescriptlang.org/docs/handbook/2/classes.html#private
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
😊About Me
Hey! I'm Deborah Kurata
I'm a software developer and KZbin content creator. I speak at conferences such as VS Live and ng-conf. I write articles for freeCodeCamp. And I'm a Pluralsight author with courses in the top 10 most popular (out of 10,000+) over the past 5 years. For my work in support of software developers, I've been recognized with the Microsoft Most Valuable Professional (MVP) award, and I'm a Google Developer Expert (GDE).
Contact me on Twitter: DeborahKurata
Find my Pluralsight courses: www.pluralsight.com/profile/author/deborah-kurata
Access my freeCodeCamp articles: www.freecodecamp.org/news/author/deborah-kurata/
View my KZbin content: www.youtube.com/@deborah_kurata
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
#angular #bestpractices #angulartutorial #demo
#typescriptprivate #privateaccessibility #javascripthash #javascriptprivate #hashvsprivate