Clean Code - Objects - Beau teaches JavaScript

**Private Members in JavaScript Objects**

In modern JavaScript development, it's essential to create private members within objects to prevent accidental direct access and modification. This ensures that sensitive data remains protected from external interference.

To demonstrate this concept, let's consider an example of a class with a constructor function that creates an initial object prototype. We can add another property to the prototype and make use of functions to interact with it.

```javascript

var account = {

balance: 0,

getBalance: function() {

return this.balance;

},

setBalance(amount) {

this.balance = amount;

}

};

```

In this example, we've created an object `account` with two properties: `balance` and the getter function `getBalance`. The `setBalance` function allows us to modify the `balance` property. We can access the `balance` property using the `getBalance` function.

To further emphasize the importance of private members, let's consider a different approach where we create an object without assigning properties directly. Instead, we return functions that provide access to these properties.

```javascript

var account = (function() {

var balance = 0;

return {

getBalance: function() {

return balance;

},

setBalance: function(amount) {

balance = amount;

}

};

})();

account.setBalance(100);

console.log(account.getBalance()); // Output: 100

```

In this revised example, we create an object using an immediately invoked function expression (IIFE). We define the `balance` variable within the IIFE and return an object with getter and setter functions. These functions provide access to the `balance` property without exposing it directly.

By using this approach, we can ensure that the `balance` property remains private and is not accessible through direct assignment or deletion. Instead, we must use the provided getter and setter functions to interact with the property.

**Best Practices for Creating Private Members**

When working with JavaScript objects, it's essential to follow best practices for creating private members. Here are some guidelines to keep in mind:

* Avoid using `this` and dot notation (e.g., `this.name`) when defining properties within an object.

* Instead, return functions that provide access to these properties.

* Use getter and setter functions to interact with private properties.

* Avoid directly deleting or modifying private properties through the object's prototype.

By adhering to these guidelines, you can create robust and secure JavaScript objects that protect sensitive data from external interference.

"WEBVTTKind: captionsLanguage: enclean code is code that is readable reusable and refactor I'm going to show you how to write clean code in objects and data structures this video is part of a series I'm doing based on an article by Ryan McDermot on clean code and JavaScript check the description for a link to the original article when creating an object it's better to use Getters and Setters to access data instead of just looking for a property on an object this is going to make it easier to change your program later it makes adding validations easier it gives increased encapsulation on your object and a few other benefits so look let's look at this code right here we have the function make bank account and when you are creating an object like this whatever you return is what you're going to be able to access in the object so here we instantiate an a bank account object and call account and then we're going to see it starts with a balance of zero whenever you make a bank account object but we're going to then set it to 100 and then we can console log that and you'll see that the balance is now 100 when it started as zero that's the bad way to do it where there's no getter and Setter you can just directly access the property in the object now let me show you the good way we are going to create a variable here and set to zero anything that's up here that's not in the return statement is private so you're not going to be able to directly access this variable now and now we're going to create a function here and another function and then we are going to return just the functions so I've created a getter function get balance and a set function set balance and then I've only returned the function you see I did not return the actual property or variable balance we've only returned the functions that means these are the only things you're going to be able to access in the object anything under the return is what is publicly available to access an the object so you can still get the balance by calling this function and it's going since this function is is going to be inside the object it can still access that variable inside the object and we can also set the balance by doing balance equals amount okay first I'm actually going to change these two these semicolons to a comma and that doesn't need a one there that was a mistake having the semicolon so account. balance instead of account. balance we're going to do account. set balance to 100 and we're going to actually instead of making it equals 100 we're going to pass in 100 and then we're going to do account. balance to get the balance and it still says 100 so that is a better way of doing it using Getters and Setters the next thing I want to talk about is that you should make objects have private members so here's the Constructor function for an employee object this Constructor function actually creates the initial object prototype but we can add another property to the Prototype and this one down here is a function so we create the employee object but we're going to add to the Prototype a get name function where it's going to return this.name so remember this is actually the bad way to do it whenever you use the word this and Dot and then you put a property name after it this.name this is creating a basically a public property that can be directly accessed so this is not this is not really a private member this a public member when you use this.name so let's see how this works I'm going to uncomment this out let me put some parentheses on there and if we run that it's going to say employee name John do employee name undefined so let's go through this code here we are going to instantiate a new employee object and call employee and we're going to pass in the name John Doe so it's going to go to this function up here it's going to it's going to do this.name equals name then the name we passed in was John Doe then we're going to console.log now these are temp literals just look in the description of this video to see a link to my other video about template literals so we have employee name and we're going to do employee. git name so we're calling this git name function now then we're going to delete employee. name and then if you console.log employee. get name again it's going to say undefined so this is the bad way of creating uh properties and and members of an object because it's easy to do something like the deleting it or you can change the name really easily we want to make it a little harder to change the name so basically I'm going to delete this whole section here and we are not even going to use the this dot notation because that creates a public property that can be easily accessed and deleted so we're going to do this so we're passing it a name but we're not assigning it to a property so it's still stored in the object but it's private so it cannot be directly accessed but we can access it using this function that's returned we're returning the git name function that's going to return the name variable that's within the object but it's not a property of the object so let's look at this down here now we're again going to do create a new employee uh called employee we're going to pass in the name John Doe and we're going to call get name let me just run this and we we'll see how it's different so you can see look employee Nam John Doe employee Nam John Doe remember last time the second name was undefined but when we try to delete employee. name well there actually is no employee. name we not create that property in the function so this is going to be a better way to create an object so the members are private and you can't accidentally delete it or change it directly you can only do it through the functions well thanks for watching my name is Bo KS don't forget to subscribe and remember use your code for goodclean code is code that is readable reusable and refactor I'm going to show you how to write clean code in objects and data structures this video is part of a series I'm doing based on an article by Ryan McDermot on clean code and JavaScript check the description for a link to the original article when creating an object it's better to use Getters and Setters to access data instead of just looking for a property on an object this is going to make it easier to change your program later it makes adding validations easier it gives increased encapsulation on your object and a few other benefits so look let's look at this code right here we have the function make bank account and when you are creating an object like this whatever you return is what you're going to be able to access in the object so here we instantiate an a bank account object and call account and then we're going to see it starts with a balance of zero whenever you make a bank account object but we're going to then set it to 100 and then we can console log that and you'll see that the balance is now 100 when it started as zero that's the bad way to do it where there's no getter and Setter you can just directly access the property in the object now let me show you the good way we are going to create a variable here and set to zero anything that's up here that's not in the return statement is private so you're not going to be able to directly access this variable now and now we're going to create a function here and another function and then we are going to return just the functions so I've created a getter function get balance and a set function set balance and then I've only returned the function you see I did not return the actual property or variable balance we've only returned the functions that means these are the only things you're going to be able to access in the object anything under the return is what is publicly available to access an the object so you can still get the balance by calling this function and it's going since this function is is going to be inside the object it can still access that variable inside the object and we can also set the balance by doing balance equals amount okay first I'm actually going to change these two these semicolons to a comma and that doesn't need a one there that was a mistake having the semicolon so account. balance instead of account. balance we're going to do account. set balance to 100 and we're going to actually instead of making it equals 100 we're going to pass in 100 and then we're going to do account. balance to get the balance and it still says 100 so that is a better way of doing it using Getters and Setters the next thing I want to talk about is that you should make objects have private members so here's the Constructor function for an employee object this Constructor function actually creates the initial object prototype but we can add another property to the Prototype and this one down here is a function so we create the employee object but we're going to add to the Prototype a get name function where it's going to return this.name so remember this is actually the bad way to do it whenever you use the word this and Dot and then you put a property name after it this.name this is creating a basically a public property that can be directly accessed so this is not this is not really a private member this a public member when you use this.name so let's see how this works I'm going to uncomment this out let me put some parentheses on there and if we run that it's going to say employee name John do employee name undefined so let's go through this code here we are going to instantiate a new employee object and call employee and we're going to pass in the name John Doe so it's going to go to this function up here it's going to it's going to do this.name equals name then the name we passed in was John Doe then we're going to console.log now these are temp literals just look in the description of this video to see a link to my other video about template literals so we have employee name and we're going to do employee. git name so we're calling this git name function now then we're going to delete employee. name and then if you console.log employee. get name again it's going to say undefined so this is the bad way of creating uh properties and and members of an object because it's easy to do something like the deleting it or you can change the name really easily we want to make it a little harder to change the name so basically I'm going to delete this whole section here and we are not even going to use the this dot notation because that creates a public property that can be easily accessed and deleted so we're going to do this so we're passing it a name but we're not assigning it to a property so it's still stored in the object but it's private so it cannot be directly accessed but we can access it using this function that's returned we're returning the git name function that's going to return the name variable that's within the object but it's not a property of the object so let's look at this down here now we're again going to do create a new employee uh called employee we're going to pass in the name John Doe and we're going to call get name let me just run this and we we'll see how it's different so you can see look employee Nam John Doe employee Nam John Doe remember last time the second name was undefined but when we try to delete employee. name well there actually is no employee. name we not create that property in the function so this is going to be a better way to create an object so the members are private and you can't accidentally delete it or change it directly you can only do it through the functions well thanks for watching my name is Bo KS don't forget to subscribe and remember use your code for good\n"