Skip to content

fix[once]: preserve this context - #2069

Merged
raon0211 merged 1 commit into
toss:mainfrom
jeonghoon11:fix/once-preserve-this-context
Aug 29, 2026
Merged

raon0211 merged 1 commit into
toss:mainfrom
jeonghoon11:fix/once-preserve-this-context

Conversation

@jeonghoon11

@jeonghoon11 jeonghoon11 commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #2068

Fixes the once function to preserve the this context of the wrapped function, matching Lodash's behavior.

Changes

The following case returns 42 in Lodash, while es-toolkit/compat returned undefined before this change because the wrapped function lost its this context.

import { once } from 'es-toolkit/compat';

const object = {
  value: 42,
  getValue: once(function () {
    return this?.value;
  }),
};

object.getValue();

// Lodash: 42
// es-toolkit/compat before this change: undefined
  • Updated the shared once implementation to forward the wrapper's this context to the original function using func.apply(this, args).
  • Added regression tests for both es-toolkit and es-toolkit/compat.

Since es-toolkit/compat reuses the main once implementation, both es-toolkit and es-toolkit/compat now return 42.

@vercel

vercel Bot commented Aug 28, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
es-toolkit Ready Ready Preview Aug 28, 2026 8:18am

Request Review

@jeonghoon11 jeonghoon11 changed the title fix(once): preserve this context fix[once]: preserve this context Aug 28, 2026
@raon0211
raon0211 merged commit a093e4b into toss:main Aug 29, 2026
12 checks passed

This branch was successfully deployed

1 active deployment
Preview — d5d98cd1 Deployed Aug 28, 2026 by vercel[bot]
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.

once does not preserve this context

2 participants