Adding support for calling function with `this` context (object/class methods)
See original GitHub issueConsider the class
export default class API {
fetch() {
console.log(this);
}
}
With backend = new API()
, the call yield call(backend.fetch)
logs undefined
, whereas yield backend.fetch()
logs the correct this
.
Edit: yield call(backend.fetch.bind(backend))
works fine, too. So feel free to close this if you don’t consider this an bug.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Can an object call on a method in the class it was instanced?
No, you can't do that; there is no relationship between 'object A' which was instantiated ( new A() ) at some point, and...
Read more >3. Data model — Python 3.11.1 documentation
When an instance method object is called, the underlying function ( __func__ ) is called, inserting the class instance ( __self__ ) in...
Read more >Defining and Calling Methods | Defining and Using Classes
A class method is a method that can be invoked without reference to any object instance; these are called static methods in other...
Read more >this - JavaScript | MDN
Class methods behave like methods in object literals — the this value is the object that the method was accessed on.
Read more >Class basic syntax - The Modern JavaScript Tutorial
So if an object method is passed around and called in another context, this won't be a reference to its object any more....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
using arrow functions instead of methods also does the trick for example: convert this code
to this
In addition: Binding the class Object itself with
yield call(MyClass.myMethod.bind(MyClass))
seems to preserve thethis
context as well, even thoughapply
should be the solution of choice.