← 返回首页
Partial application for login
EN

We want to make this open-source project available for people all around the world.

Help to translate the content of this tutorial to your language!

    Search
    Search
    Light themeDark theme
    عربيDanskEnglishEspañolفارسیFrançaisIndonesiaItaliano日本語한국어РусскийTürkçeУкраїнськаOʻzbek简体中文
    back to the lesson

    Partial application for login

    importance: 5

    The task is a little more complex variant of Fix a function that loses "this".

    The user object was modified. Now instead of two functions loginOk/loginFail, it has a single function user.login(true/false).

    What should we pass askPassword in the code below, so that it calls user.login(true) as ok and user.login(false) as fail?

    function askPassword(ok, fail) { let password = prompt("Password?", ''); if (password == "rockstar") ok(); else fail(); } let user = { name: 'John', login(result) { alert( this.name + (result ? ' logged in' : ' failed to log in') ); } }; askPassword(?, ?); // ?

    Your changes should only modify the highlighted fragment.

    solution
    1. Either use a wrapper function, an arrow to be concise:

      askPassword(() => user.login(true), () => user.login(false));

      Now it gets user from outer variables and runs it the normal way.

    2. Or create a partial function from user.login that uses user as the context and has the correct first argument:

      askPassword(user.login.bind(user, true), user.login.bind(user, false));