FREELessons: 13Length: 1.7 hours

• Overview
• Transcript

# 3.4 Privacy

We’ve seen how to implement code privacy (encapsulation) with the Symbol data type, but unfortunately it isn’t polyfillable. But WeakMap is, and it’s perfect for our privacy needs.

## 6.Conclusion1 lesson, 00:43

### 3.4 Privacy

In the previous lessons we have incorporated privacy into our person data types. And that's a wonderful thing. But unfortunately, as they are currently implemented, we can't really use them except within the latest browsers. Primarily, because the latest browsers support symbols, whereas older browsers do not. And unfortunately, it's not a feature that we can pollyfill. I mean, due to the nature of a symbol, it's designed to be private. And, if we don't truly have privacy right now, then, how can we implement that? With a symbol. However there is another feature in ES2015 called a WeakMap. Now, not every browser supports it but it is polyfillable. So, we can use it in older browsers. Now we can't use it in IE 8. But really anything after IE 8s, we can use it. And we can implement privacy, in almost the same way. We just have to create a WeakMap. And then, map the data to the object that we are creating. And that's what a WeakMap is, it's a storage device for key valued pairs where the key is an object. And it has to be an object, you cannot use a WeakMap where the key is a primitive data type. So, let's start by removing the symbols that we created earlier. And we're going to create a WeakMap, I'm just going to call it map. And we want to new up the WeakMap constructor. And inside of our person constructor, we will get rid of the code that we have here, and instead, we're going to use our [INAUDIBLE]. It has a method called sets, and then, we pass in the object that we are building. So, for the key in this map. It is the parson object. And then, the value can be anything, and it's going to be an object in our case. And the value is going to contain the first name, and the last name values. So,we will have first name, is going to have the firstName value. And then, we will do the same thing for the lastName. And then, we will be able to retrieve these any time that we want to get the firstName. And the lastName. Like for example, inside of the greet method where we have used this, and then, the firstName symbol, well we'll just get rid of that and instead, we'll use our WeakMap. And has a method called get, we pass in the this object, and then, we use the firstName property. And we will do the same thing inside of our firstName property. So, instead of return this, and then, firstName symbol, we use our WeakMap. We get the value for this, which is an object that has the firstName property. For the lastName, same thing, except that this is going to be for the lastName. And then finally, for the renamePerson static method. If you guessed that we will do is the same thing, you are correct. Instead of using the lastName symbol, we are going to use our WeakMap to get to the lastName, and set that to the new value. And with just, those very simple modifications. We have essentially implemented privacy, but with technology that we can use today. Now, the great thing about a WeakMap is that the values are weakly mapped to the keys. So, whenever we de-reference our person objects, we don't have to worry about any lingering references. The values Mapped to that person object is going to go away as well. So, it is a very safe way of implementing privacy today.

Back to the top