DEV Community

THE CODE DOCTOR
THE CODE DOCTOR

Posted on • Edited on

2 2

Was ist der Unterschied zwischen der Call- und Apply-Methode?

Der Unterschied zwischen call u. apply besteht darin, wie die Argumente an die aufgerufene Funktion übergeben werden. In apply werden die Argumente als Array und bei call getrennt durch ein Komma übergeben.

const object1 = {
    result: 0
}
const object2 = {
    result: 0
}
function reduceAdd() {
    let result = 0
    for (let i = 0, len = arguments.length; i< len; i++){
        result += arguments[i]
    }
    this.result = result
}
reduceAdd.apply(object1, [1, 2, 3, 4, 5, 6]) //21
reduceAdd.call(object2, 1, 2, 3, 4, 5, 6) //21
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more