From the course: Learning Functional Programming with JavaScript ES6+
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Implement private variable - JavaScript Tutorial
From the course: Learning Functional Programming with JavaScript ES6+
Implement private variable
- [Instructor] So the most common way that I see programmers implementing private variables in JavaScript is by simply adding an underscore at the beginning of their variable name. For example, _name. This is supposed to signify to themselves and to other developers that you should never directly use this variable outside the class. However, in my experience programmers will always ignore this when it's convenient for them. So let's look at a better way. Let's imagine that we want to create a person class. Now instead of using the class keyword, as many people are used to doing, we're just going to define it as a function that returns a new person object. And that'll look like this. We'll say const Person = function. And then instead of passing multiple arguments to specify name, age, and job for this person, we're going to use object destructuring. And we're going to have name, age, and job properties. And then we're going to define the function body here, and at some point inside…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.