Предположим, что уже существуют и объект, и функция. function f(){} var o = {}; Выполнение кода: f.call(o, 1, 2); Равносильно следующему коду: om = f; om(1, 2); delete om; 

Along the way, learning js stumbled upon this method, but essentially I can not understand. Maybe someone will give a good guide or explain what the joke is?

  • 2
    Your example can be interpreted here as - of (1, 2); That is, the first parameter of the call method? this is the context, the context is this, this is HERE! It turns out that by passing the object o to the context, you are currently linking the method with the object. Also it turns out of (1, 2); And at the time of this call, this in method f will refer to the scope object o. - vas
  • one
    @vas, a small amendment: this is "this" and "here" this is here. - zb '

1 answer 1

The joke here is in the manual setting of the function context. The context is the object this inside a function.

 function f(x) { alert(this); } var o1 = '123'; var o2 = '456'; f.call(o1, 0); // выведет 123 f.call(o2, 0); // выведет 456 

If the context is not set, then the window object will become it. An example of the use of a context change is jQuery. In this library, in each callback, the context is set to the object with which the callback works:

 $('#btn').click(function() { console.log(this); // здесь this - это элемент #btn } 
  • and 0 in the call what role plays? well, and F (o1); the same thing will bring out .... - msim