Back to tasks
СБЕРGet help with live coding in real time with Sobes Copilot
Junior — Senior
24
Исследование контекста this при разных способах вызова функций
Companies where asked:
Task condition
В этом примере показано, как значение this меняется в обычных функциях, методах объекта и стрелочных функциях. Продемонстрированы прямые вызовы, а также использование call, apply и bind, включая попытку пере‑привязать уже связанную функцию.
const methodKey = "fn3";
const container = {
fn1: function () {
console.log(this);
const f = () => console.log(this);
f();
},
fn2() {
console.log(this);
},
[methodKey]: () => console.log(this),
};
console.log(this); //
container.fn1(); //
container.fn2(); //
container[methodKey](); //
const external = {
new: 123,
};
const boundFn = container.fn1.bind(external);
boundFn.bind(this); //
container.fn2.call(external); //
container[methodKey].apply(external); //