Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
52
rated 0 times [  57] [ 5]  / answers: 1 / hits: 17054  / 6 Years ago, wed, june 6, 2018, 12:00:00

I am working on a React Native project. On some tutorials I have seen to bind a method like this:



constructor(props){
super(props);
this.my_function = this.my_function.bind(this);
}


My question is if I can access the function in constructor using this.my_function then why I need to bind this again? I have Java and Python background, may be that's why I am confused with this type of method binding.
Note: I know that if I don't bind a method in React Native/React JS my method won't work correctly. I just want to know why I need this extra binding.


More From » reactjs

 Answers
14

Take a look at this link to understand how JS closure works



http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/



You need to bind the function so it has access to state and other variables in your class, not just parameters that pass when you execute.


[#54260] Saturday, June 2, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
patienceannel

Total Points: 674
Total Questions: 101
Total Answers: 101

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
patienceannel questions
Fri, Mar 11, 22, 00:00, 2 Years ago
Tue, Oct 20, 20, 00:00, 4 Years ago
Wed, Jul 24, 19, 00:00, 5 Years ago
;