7 Mayıs 2019 Salı

Function call metodu

Giriş
Açıklaması şöyle.
When you have a reference of the function in hand, you can call the function with the context provided by you. It can be done by using two methods that functions provide:

- call - it takes a context as the first argument. The rest of arguments are arguments passed to the function being called this way.
- apply - it takes a context as the first argument and an array of arguments for the function being called as the second argument.
this Parametresi verilmemesi
Açıklaması şöyle. Eğer call() metoduna parametre verilmezse window nesnesi kullanılır.
thisArg

Optional. The value of this provided for the call to a function. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode, null and undefined will be replaced with the global object and primitive values will be converted to objects.
Örnek
Şöyle yaparız.
function foo(){
  console.log(this);
}
foo.call(foo); //foo function
foo.call(); //window object
Örnek
Şöyle yaparız.
var f = function() {
  this.x = 5;
  (function() {
    this.x = 3;
  })();
  console.log(this.x);
};

f.call(f); //5

f(); //3

f.call(); //3

Hiç yorum yok:

Yorum Gönder