Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop unnecessary binding #12

Merged
merged 3 commits into from
Jan 13, 2021
Merged

Drop unnecessary binding #12

merged 3 commits into from
Jan 13, 2021

Conversation

fregante
Copy link
Contributor

@fregante fregante commented Jan 12, 2021

this here is always window/global, which means that if the functions actually use this, they will refer to window/global… which is the default.

Am I missing something?

Edit: I suppose this was just to apply the parameters before the spread operator became available.

@fregante fregante marked this pull request as ready for review January 13, 2021 01:02
@sindresorhus
Copy link
Owner

Edit: I suppose this was just to apply the parameters before the spread operator became available.

Correct

@sindresorhus sindresorhus merged commit b59d1cf into sindresorhus:master Jan 13, 2021
@fregante fregante deleted the patch-1 branch January 13, 2021 21:58
@darcyparker
Copy link
Contributor

darcyparker commented Jan 19, 2021

I just traced a bug in my code to this change that dropped the unnecessary binding. I think it should be reconsidered and reverted because I'd argue it is necessary is some contexts.

It is true that this always globalThis/window IF the setTimeout() argument was a function. But its not a function - it is an arrow function. this should be the same this of the returned function (https://github.com/sindresorhus/p-debounce/blob/master/index.js#L12)

Consider:

class Something {
  private abc: string = '123'
   fn() {
     console.log(this.abc);
   }
}

Something.prototype.fn = debounce(Something.prototype.fn, 100);
const b = new Something();
b.fn(); //An error will be thrown with this pull request's change because `this` is not `b` instance.

In this case the debounced Something.prototype.fn, this should not be globalThis.

Would you agree? And would you consider a pull request that adds unit test along the lines of my example?

darcyparker added a commit to darcyparker/p-debounce that referenced this pull request Jan 20, 2021
darcyparker added a commit to darcyparker/p-debounce that referenced this pull request Jan 20, 2021
* This reverts sindresorhus#12
* And it addresses same problem with pDebounce.promise() implementation
  added in sindresorhus#13
@darcyparker
Copy link
Contributor

See #16
Thanks for reviewing my comment and giving thumbs up @fregante

darcyparker added a commit to darcyparker/p-debounce that referenced this pull request Jan 20, 2021
darcyparker added a commit to darcyparker/p-debounce that referenced this pull request Jan 20, 2021
* This reverts sindresorhus#12
* And it addresses same problem with pDebounce.promise() implementation
  added in sindresorhus#13
darcyparker added a commit to darcyparker/p-throttle that referenced this pull request Jan 20, 2021
* This is to protect against a mistake like
  sindresorhus/p-debounce#12 (comment)
* Similar to sindresorhus/p-debounce#16, but for
  pThrottle.
darcyparker added a commit to darcyparker/p-throttle that referenced this pull request Jan 20, 2021
* Add comments about importance of preserving `this` to avoid future
  mistakes like sindresorhus/p-debounce#12 (comment)
darcyparker added a commit to darcyparker/p-debounce that referenced this pull request Jan 21, 2021
* This reverts sindresorhus#12
* And it addresses same problem with pDebounce.promise() implementation
  added in sindresorhus#13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants