Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Исправление ошибки в функции карирования#2038

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Mokss wants to merge1 commit intojavascript-tutorial:master
base:master
Choose a base branch
Loading
fromMokss:patch-2

Conversation

Mokss
Copy link

Описание

В примере учебника функции карирования явно добавлен метод apply, указывающий, что мы должны помнить о контексте. Но пример не учел, что возвращаемую рекурсивно функцию curried так же оборачивают в анонимную функцию, у которой свой контекст this, из-за чего происходит потеря изначального контекста и пример работает не правильно.
Замена анонимной функции на стрелочную решает проблему, так как стрелочная функция не имеет своего this, и берет его у своего родителя.

Вот пример где ошибку легко воспроизвести

functioncurry(func){returnfunctioncurried(...args){if(args.length>=func.length){returnfunc.apply(this,args);}else{returnfunction(...args2){returncurried.apply(this,args.concat(args2));}}};}functiontestFn(biba,boba){returnthis.prefix+biba+', '+boba;}constobj={prefix:"Mokss: ",test:curry(testFn)};console.log(obj.test("Biba")("Boba"));// undefinedBiba, Boba

Пример с фиксом, где код работает ожидаемо

functioncurry(func){returnfunctioncurried(...args){if(args.length>=func.length){returnfunc.apply(this,args);}else{return(...args2)=>{returncurried.apply(this,args.concat(args2));}}};}functiontestFn(biba,boba){returnthis.prefix+biba+', '+boba;}constobj={prefix:"Mokss: ",test:curry(testFn)};console.log(obj.test("Biba")("Boba"));// Mokss: Biba, Boba

meowcorp and ya-kostik reacted with thumbs up emoji
@javascript-translate-botjavascript-translate-bot added review neededReview needed, please approve or request changes labelsApr 5, 2025
@javascript-translate-botjavascript-translate-bot requested a review froma teamApril 5, 2025 17:15
@CLAassistant
Copy link

CLAassistant commentedApr 5, 2025
edited
Loading

CLA assistant check
All committers have signed the CLA.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
review neededReview needed, please approve or request changes
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@Mokss@CLAassistant@javascript-translate-bot

[8]ページ先頭

©2009-2025 Movatter.jp