diff --git a/index.js b/index.js index 6d6a826..abf91e6 100644 --- a/index.js +++ b/index.js @@ -40,8 +40,12 @@ function debounce(function_, wait = 100, options = {}) { } const debounced = function (...arguments_) { - if (storedContext && this !== storedContext) { - throw new Error('Debounced method called with different contexts.'); + if ( + storedContext && + this !== storedContext && + Object.getPrototypeOf(this) !== Object.getPrototypeOf(storedContext) + ) { + throw new Error('Debounced method called with different contexts of the same prototype.'); } storedContext = this; // eslint-disable-line unicorn/no-this-assignment diff --git a/test.js b/test.js index 054602e..c014784 100644 --- a/test.js +++ b/test.js @@ -162,7 +162,7 @@ test('context check in debounced function', async t => { instance2.debounced(); } catch (error) { errorThrown = true; - assert.strictEqual(error.message, 'Debounced method called with different contexts.', 'Error message should match'); + assert.strictEqual(error.message, 'Debounced method called with different contexts of the same prototype.', 'Error message should match'); } assert.ok(errorThrown, 'An error should have been thrown');