|
|
|
|
|
by baron816
3361 days ago
|
|
I hate that the React team prefers ES6 classes. This is what I do: function App(params) {
const component = new React.Component(params); component.lifeCycleMethod = function() {...};
component.render = function() {...};
function privateMethod() {...}
return component;
}
|
|
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.]