Hacker News new | past | comments | ask | show | jobs | submit login

This is the RevealingModulePattern that Crockford used to advocate back in the day, except assigning to an object that React creates instead of one you create..

http://javascript.crockford.com/private.html

https://addyosmani.com/resources/essentialjsdesignpatterns/b...

Be aware that memory usage can quickly balloon with this pattern, because the GC needs to keep alive anything referenced from inside the closure, including closure objects for the private methods for each individual instantiation of the component. With normal prototypal inheritance, there's only one function object per class; that's the upside of the explicit 'this'. This (and inability of early debuggers to inspect closure variables, which has since been fixed) were what killed this technique in the 2000s.

[Edit: parent post was edited to remove the code sample. I'll keep this up since apparently people are finding it informative, but be aware that I'm replying to the code sample that used to be in the parent comment, not anything in the article.]




Sorry, I was getting down voted a lot. I restored the code.

I'm pretty sure Crockford still pushes this. https://weblogs.asp.net/bleroy/crockford%E2%80%99s-2014-obje...

It does eat up more memory, but as Crockford says, memory is cheap. It's not likely that it'll cause a problem.


Memory is not cheap for your users. If your webapp (or worse webpage) burns through their memory they will come to regard it as slow, bloated and heavy and close the tab never to return.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: