Az apply()
metódussal olyan metódust írhat, amely különböző tárgyakat.
apply()
módszerAz apply()
metódus hasonló a call()
metódushoz (előző fejezet).
Ebben a példában a person fullName metódusa alkalmazott a person1-re:
const person = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const person1 = {
firstName: "Mary",
lastName: "Doe"
}
// This will return "Mary Doe":
person.fullName.apply(person1);
Próbálja ki Ön is →
<!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()
és az apply()
közöttA különbség a következő:
A call()
metódus külön veszi az argumentumokat.
Az apply()
metódus az argumentumokat tömbként veszi.
Az apply() metódus nagyon hasznos, ha argumentumlista helyett tömböt szeretnénk használni.
apply()
módszer argumentumokkalAz apply()
metódus argumentumokat fogad el egy tömbben:
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"]);
Próbálja ki Ön is →
<!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>
Összehasonlítva a call()
metódussal:
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");
Próbálja ki Ön is →
<!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>
A legnagyobb számot (a számlistában) a Math.max()
metódussal találhatja meg:
Math.max(1,2,3); // Will return 3
Próbálja ki Ön is →
<!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>
Mivel a JavaScript tömbök nem rendelkeznek max() metódussal, alkalmazhatja a Math.max()
metódus helyett.
Math.max.apply(null, [1,2,3]); // Will also return 3
Próbálja ki Ön is →
<!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>
Az első argumentum (null) nem számít. Ebben a példában nem használjuk.
Ezek a példák ugyanazt az eredményt adják:
Math.max.apply(Math, [1,2,3]); // Will also return 3
Próbálja ki Ön is →
<!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 3
Próbálja ki Ön is →
<!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 3
Próbálja ki Ön is →
<!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 szigorú módban, ha az apply()
metódus első argumentuma nem objektum, a meghívott függvény tulajdonosává (tárgyává) válik. "Nem szigorú" módban ez lesz a globális objektum.