JavaScript-funktion apply() Metode


Indholdsfortegnelse

    Vis indholdsfortegnelse


Metode Genbrug

Med apply() metoden kan du skrive en metode, der kan bruges på forskellige genstande.


JavaScript-metoden apply()

Metoden apply() ligner metoden call() (forrige kapitel).

I dette eksempel er fullName-metoden for person anvendtperson1:

Eksempel

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øv det selv →

<!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>



Forskellen mellem call() og apply()

Forskellen er:

Metoden call() tager argumenter separat.

Metoden apply() tager argumenter som en matrix.

Apply()-metoden er meget praktisk, hvis du vil bruge et array i stedet for en argumentliste.


Metoden apply() med argumenter

Metoden apply() accepterer argumenter i et array:

Eksempel

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øv det selv →

<!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>


Sammenlignet med metoden call():

Eksempel

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øv det selv →

<!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>




Simuler en Max-metode på arrays

Du kan finde det største tal (på en liste over tal) ved at bruge Math.max() metoden:

Eksempel

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

Prøv det selv →

<!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>


Da JavaScript arrays ikke har en max() metode, kan du anvende Math.max()-metoden i stedet.

Eksempel

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

Prøv det selv →

<!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>



Det første argument (nul) er ligegyldigt. Det bruges ikke i dette eksempel.

Disse eksempler vil give det samme resultat:

Eksempel

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

Prøv det selv →

<!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>



Eksempel

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

Prøv det selv →

<!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>



Eksempel

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

Prøv det selv →

<!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 streng tilstand

I streng JavaScript-tilstand, hvis det første argument i apply()-metoden ikke er et objekt, den bliver ejer (objekt) af den påkaldte funktion. I "ikke-streng" tilstand bliver det det globale objekt.