question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding support for calling function with `this` context (object/class methods)

See original GitHub issue

Consider 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:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
murtrajacommented, Dec 13, 2018

using arrow functions instead of methods also does the trick for example: convert this code

export default class API {
  fetch() {
    console.log(this);
  }
}

to this

export default class API {
  fetch = () => {
    console.log(this);
  }
}
4reactions
voodoodscommented, Nov 9, 2017

In addition: Binding the class Object itself with yield call(MyClass.myMethod.bind(MyClass)) seems to preserve the this context as well, even though apply should be the solution of choice.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found