As crockford says “If a value is a function, we can consider it a method”. Let us see a simple example that shows how to use a closure and hide a member.
![JavaScript private method JavaScript private method](http://lh3.ggpht.com/-gvs7Mf-YxO8/Thu7n1k8NAI/AAAAAAAACFw/P7oWbZhSMas/image%25255B7%25255D.png?imgmax=800)
Any attempt to call deleteEmployee will result in the following error:
![JavaScript private method JavaScript private method](http://lh3.ggpht.com/-eVSK3u4C-Pw/Thu7oiW5guI/AAAAAAAACF0/DdMIprVXCNE/image%25255B2%25255D.png?imgmax=800)
Just keep in mind that by doing it this way, it cannot be a part of the prototype. Call it a hack, trick or a hidden feature, nevertheless it is a very useful tip to hide information inside a JavaScript Object!
Tweet
No comments:
Post a Comment