function etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
function etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

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

22 Ocak 2019 Salı

Function Sınıfı

Function.prototype.apply metodu
Şöyle yaparız.
// Create constructor method for array of args
Function.prototype.construct = function(aArgs) {
  var oNew = Object.create(this.prototype);
  this.apply(oNew, aArgs);
  return oNew;
};

function Person(name, gender) {
    this.name = name;
    this.gender = gender;
}

var args = ["Helen", "F"];

var person = Person.construct(args);

console.log(person);

14 Ağustos 2018 Salı

function Anahtar Kelimesi

1. Function Declaration
Sadece function tanımlanır. Çağrılamaz.
Örnek
Function's isim verilmelidir. Şöyle yaparsak SyntaxError alırız.
//gives `SyntaxError`
function() {
    console.log('Inside the function');
}();
Örnek
Function'a isim versek bile şöyle yaparsak SyntaxError alırız. Çünkü Function Declaration çalıştırılamaz.
function func(){
  console.log('x')
}();
Function Declaration'ı Hemen Çalıştırmak
Function Declaration parantez içine alınır.
Örnek
Şöyle yaparız
(function() {
    console.log('Inside the function');
})();
Örnek
Şöyle yaparız.
(function(){console.log("abc");})();

(function(){console.log("123");})();
2. Function Expression
Function Declaration bir değişkene atanır ve çağrılabilir hale gelir.
Örnek
Şöyle yaparız
+function(){
  console.log('done')
}()
Örnek
Şöyle yaparız.
// Executes without any error
var x = function() {console.log('Inside the function')}(); // Inside the function