जावास्क्रिप्ट में call() और apply() के साथ फ़ंक्शन्स को इनवोक करने का कोड निम्नलिखित है -
उदाहरण
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: blueviolet; } </style> </head> <body> <h1>call() और apply() के साथ फ़ंक्शन्स इनवोक करना</h1> <div class="result"></div> <br /> <button class="Btn">यहाँ क्लिक करें</button> <h3>call() और apply() मेथड के साथ फ़ंक्शन्स इनवोक करने के लिए ऊपर दिए गए बटन पर क्लिक करें</h3> <script> let resEle = document.querySelector(".result"); let BtnEle = document.querySelector(".Btn"); function addNum(num1, num2, num3, num4) { return num1 + num2 + num3 + num4; } function multiplyNum(num1, num2, num3, num4) { return num1 * num2 * num3 * num4; } let arr = [22, 33, 44, 55]; BtnEle.addEventListener("click", () => { resEle.innerHTML = "संख्याओं का योग = " + addNum.call(this, 22, 33, 44, 55) + "<br>"; resEle.innerHTML += "ऐरे एलिमेंट्स का गुणनफल = " + multiplyNum.apply(this, arr) + "<br>"; }); </script> </body> </html>
आउटपुट

'यहाँ क्लिक करें' बटन पर क्लिक करने पर -
