ด้วยเมธอด apply()
คุณสามารถเขียนเมธอดที่สามารถใช้กับเมธอดอื่นได้ วัตถุ
apply()
วิธีการapply()
วิธีการจะคล้ายกับ call()
วิธีการ (บทที่แล้ว)
ในตัวอย่างนี้ วิธีการ fullName ของ บุคคล ถูก นำไปใช้ กับ person1:
const person = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const person1 = {
firstName: "Mary",
lastName: "Doe"
}
// This will return "Mary Doe":
person.fullName.apply(person1);
ลองด้วยตัวคุณเอง →
<!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()
และ apply()
ความแตกต่างคือ:
call()
วิธีการรับอาร์กิวเมนต์ แยกกัน
apply()
วิธีการรับอาร์กิวเมนต์เป็น อาร์เรย์
เมธอด Apply() มีประโยชน์มากหากคุณต้องการใช้อาร์เรย์แทนรายการอาร์กิวเมนต์
apply()
วิธีการที่มีอาร์กิวเมนต์apply()
วิธีการยอมรับอาร์กิวเมนต์ในอาร์เรย์:
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"]);
ลองด้วยตัวคุณเอง →
<!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()
วิธีการ:
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");
ลองด้วยตัวคุณเอง →
<!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>
คุณสามารถค้นหาจำนวนที่มากที่สุด (ในรายการตัวเลข) ได้โดยใช้วิธี Math.max()
:
Math.max(1,2,3); // Will return 3
ลองด้วยตัวคุณเอง →
<!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 ไม่มีเมธอด max() คุณจึงสามารถใช้ ให้ใช้วิธี Math.max()
แทน
Math.max.apply(null, [1,2,3]); // Will also return 3
ลองด้วยตัวคุณเอง →
<!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>
อาร์กิวเมนต์แรก (null) ไม่สำคัญ มันไม่ได้ใช้ในตัวอย่างนี้
ตัวอย่างเหล่านี้จะให้ผลลัพธ์เดียวกัน:
Math.max.apply(Math, [1,2,3]); // Will also return 3
ลองด้วยตัวคุณเอง →
<!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
ลองด้วยตัวคุณเอง →
<!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
ลองด้วยตัวคุณเอง →
<!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 หากอาร์กิวเมนต์แรกของเมธอด apply()
ไม่ใช่อ็อบเจ็กต์ มันจะกลายเป็นเจ้าของ (วัตถุ) ของฟังก์ชันที่ถูกเรียกใช้ ในโหมด "ไม่เข้มงวด" มันจะกลายเป็นอ็อบเจ็กต์โกลบอล