Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Prototypal modularity

prototype.jpg

The sample protolib.js has a few ideas at work. It shows how strictly private data can be kept entirely off of the object instances, using an identifier on each object to map them to their private data counterparts. This can be used when we don't want consumers accessing particular pieces of private data.

Secondly, we can use Object.defineProperty as a way to define read-only properties, like we did with the _id in this case, to avoid consumers changing (or deleting) them and breaking the private data reference, or our library code in general.

Thirdly there is an IIFE being used here too, that helps us hide the private data entirely, both from the instances and the global object. In addition, functions or variables which have no business being on the global object can be properly contained in our module's code, such is the case of the lastId variable, and the done function.