This question is similar to When using React Is it preferable to use fat arrow functions or bind functions in constructor? but a little bit different. You can bind a function to this
in the constructor, or just apply arrow function in constructor. Note that I can only use ES6 syntax in my project.
1.
class Test extends React.Component{
constructor(props) {
super(props);
this.doSomeThing = this.doSomeThing.bind(this);
}
doSomething() {}
}
2.
class Test extends React.Component{
constructor(props) {
super(props);
this.doSomeThing = () => {};
}
}
What's the pros and cons of these two ways? Thanks.
I think you may want like this. It is the same with your first situation. it will work in stage-2 with babel. (transform-class-properties : http://babeljs.io/docs/plugins/transform-class-properties/) (preset-stage-2: http://babeljs.io/docs/plugins/preset-stage-2/)
Check this out:
https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact%2Cstage-2&targets=&browsers=&builtIns=false&debug=false&code=class%20Dog%20%7B%0A%20%20constructor()%20%7B%0A%20%20%20%20%0A%20%20%20%20this.cat%20%3D%20_%3D%3E%20%7B%0A%20%20%20%20%20%20this%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D
We can see babel transpiles
into
edit: I misread your post slightly, but the above is still true and interesting. @CodinCat points out the important thing: declaring a function inside the constructor means it takes time (albeit very little) to add that function to the object when it is created, and also probably takes memory because instances of that class don't share the same doSomeThing method.
edit2: binding (this) to the function actually causes the exact issues I listed above. In other words the two methods are almost exactly the same.
Approach 1 is more readable for me and more idiomatic.
Besides, declaring methods inside a class instead of constructor, the methods can be shared.
In approach 2, you will declare all the methods every time you create a new instance:
Theoretically the approach 2 is slower, but the impact on performance should be negligible.
I'll simply go for approach 1, since it's used in the React documentation, also I never saw anyone use approach 2.
I just ran some samples to test the performance. In latest Chrome (Mac), declaring methods in constructor is about 90% slower than using
bind
in constructor.Option 1 is generally more preferable for certain reasons.
Prototype method is cleaner to extend. Child class can override or extend
doSomething
withWhen instance property
or ES.next class field
are used instead, calling
super.doSomething()
is not possible, because the method wasn't defined on the prototype. Overriding it will result in assigningthis.doSomeThing
property twice, in parent and child constructors.Prototype methods are also reachable for mixin techniques:
Prototype methods are more testable. They can be spied, stubbed or mocked prior to class instantiation:
This allows to avoid race conditions in some cases.