[Javascript] Required function arguments in Javascript

In Javascript, all function arguments are optional by default. That means if you ever forget to pass a critical parameter, the code will just fail without warning you what went wrong.

There are many workarounds for this, and in this lesson, you will learn one of the easiest and foolproof methods of solving this problem.

 
const required = function(param){
    throw new Error(`
        ${param} is not defined
    `)
}

const Division = function(
    a=required("a"),
    b=required("b")
){
    return a/b
}

console.log(Division(10, 2))
原文地址:https://www.cnblogs.com/Answer1215/p/8330229.html