JavaScript Fonksiyonu application() Yöntemi


İçindekiler

    İçindekiler tablosunu göster


Yöntemin Yeniden Kullanımı

apply() yöntemi ile farklı platformlarda kullanılabilecek bir yöntem yazabilirsiniz. nesneler.


JavaScript apply() Yöntemi

apply() yöntemi, call() yöntemine benzer (önceki bölüm).

Bu örnekte kişi'nin fullName yöntemi kişi1'e uygulanmıştır:

Örnek

const person = {
    fullName: function() {
      return this.firstName + " " + this.lastName;
    }
}

const person1 = {
  firstName: "Mary",
  lastName: "Doe"
}

// This will return "Mary Doe":
person.fullName.apply(person1);

Kendiniz Deneyin →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>
<p>In this example the fulllName method of person is <b>applied</b> on person1:</p>

<p id="demo"></p>

<script>
const person = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}

const person1 = {
  firstName:"John",
  lastName: "Doe"
}

document.getElementById("demo").innerHTML = person.fullName.apply(person1); 
</script>

</body>
</html>



call() ve apply() Arasındaki Fark

Fark şudur:

call() yöntemi bağımsız değişkenleri ayrı ayrı alır.

apply() yöntemi, bağımsız değişkenleri bir dizi olarak alır.

Bir argüman listesi yerine bir dizi kullanmak istiyorsanız, application() yöntemi çok kullanışlıdır.


Argümanlarla apply() Yöntemi

apply() yöntemi bir dizideki bağımsız değişkenleri kabul eder:

Örnek

const person = {
    fullName: function(city, country) {
    return this.firstName + " " + this.lastName 
  + "," + city + "," + country;
    }
}

const person1 = {
  firstName:"John",
    lastName: "Doe"
}

person.fullName.apply(person1, ["Oslo", "Norway"]);

Kendiniz Deneyin →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>
<p>In this example the fulllName method of person is <b>applied</b> on person1:</p>

<p id="demo"></p>

<script>
const person = {
  fullName: function(city, country) {
    return this.firstName + " " + this.lastName + "," + city + "," + country;
  }
}

const person1 = {
  firstName:"John",
  lastName: "Doe"
}

document.getElementById("demo").innerHTML = person.fullName.apply(person1, ["Oslo", "Norway"]); 
</script>

</body>
</html>


call() yöntemiyle karşılaştırıldığında:

Örnek

const person = {
  fullName: function(city, country) {
    return this.firstName + " " + this.lastName 
  + "," + city + "," + country;
    }
}

const person1 = {
    firstName:"John",
    lastName: "Doe"
}

person.fullName.call(person1, "Oslo", "Norway");

Kendiniz Deneyin →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>
<p>This example calls the fullName method of person, using it on person1:
</p>

<p id="demo"></p>

<script>
const person = {
  fullName: function(city, country) {
    return this.firstName + " " + this.lastName + "," + city + "," + country;
  }
}

const person1 = {
  firstName:"John",
  lastName: "Doe"
}

const person2 = {
  firstName:"Mary",
  lastName: "Doe"
}

document.getElementById("demo").innerHTML = person.fullName.call(person1, "Oslo", "Norway"); 
</script>

</body>
</html>




Dizilerde Maksimum Yöntemi Simüle Etme

En büyük sayıyı (sayı listesinde) Math.max() yöntemini kullanarak bulabilirsiniz:

Örnek

Math.max(1,2,3);  // Will return 3

Kendiniz Deneyin →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math.max()</h2>
<p>This example returns the highest number in a list of number arguments:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.max(1,2,3); 
</script>

</body>
</html>


JavaScript dizileri max() yöntemine sahip olmadığından, şu yöntemi uygulayabilirsiniz: Bunun yerine Math.max() yöntemini kullanın.

Örnek

Math.max.apply(null, [1,2,3]); // Will also return 3

Kendiniz Deneyin →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript apply()</h2>
<p>This example returns the highest number in an array of numbers:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.max.apply(null, [1,2,3]); 
</script>

</body>
</html>



İlk argüman (null) önemli değil. Bu örnekte kullanılmamıştır.

Bu örnekler aynı sonucu verecektir:

Örnek

Math.max.apply(Math, [1,2,3]); // Will also return 3

Kendiniz Deneyin →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript apply()</h2>
<p>This example returns the highest number in an array of numbers:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.max.apply(Math, [1,2,3]); 
</script>

</body>
</html>



Örnek

Math.max.apply(" ", [1,2,3]); // Will also return 3

Kendiniz Deneyin →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript apply()</h2>
<p>This example returns the highest number in an array of numbers:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.max.apply(" ", [1,2,3]); 
</script>

</body>
</html>



Örnek

Math.max.apply(0, [1,2,3]); // Will also return 3

Kendiniz Deneyin →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript apply()</h2>
<p>This example returns the highest number in an array of numbers:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.max.apply(0, [1,2,3]); 
</script>

</body>
</html>




JavaScript Katı Modu

JavaScript katı modunda, apply() yönteminin ilk argümanı bir nesne değilse, çağrılan işlevin sahibi (nesnesi) olur. "Katı olmayan" modda global nesne haline gelir.