First, I made a basic closure around an object literal so I can pass in some variables during instantiation: (GUY1)
Running this code in the console will allow you to see that Bob keeps a pair of socks in box zero:
Once he keeps the socks, he won’t share them (he will not pass the sock object back), only show a JSON representation of his socks. In Netbeans, we can see that our constructor function is accurately identified as a class:
However, it’s not obvious that bob is an instance of Guy because we didn’t use the “new” operator. We have another problem with Bob. Despite the fact that we told him not to share his socks with others, his socks are not encapsulated–anyone can take them or change them:
While Bob may not care too much if someone else wears his socks, he would care in this scenario:
Someone who knew where Bob keeps his cash was able to set him to zero dollars. Sad Bob.