apply() yöntemi ile farklı platformlarda kullanılabilecek bir yöntem yazabilirsiniz. nesneler.
apply() Yöntemiapply() yöntemi, call() yöntemine benzer (önceki bölüm).
Bu örnekte kişi'nin fullName yöntemi kişi1'e uygulanmıştır:
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 FarkFark ş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.
apply() Yöntemiapply() yöntemi bir dizideki bağımsız değişkenleri kabul eder:
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:
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>
En büyük sayıyı (sayı listesinde) Math.max() yöntemini kullanarak bulabilirsiniz:
Math.max(1,2,3); // Will return 3Kendiniz 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.
Math.max.apply(null, [1,2,3]); // Will also return 3Kendiniz 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:
Math.max.apply(Math, [1,2,3]); // Will also return 3Kendiniz 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>
Math.max.apply(" ", [1,2,3]); // Will also return 3Kendiniz 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>
Math.max.apply(0, [1,2,3]); // Will also return 3Kendiniz 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ı 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.