/**
* Called if a parameter is missing and
* the default value is evaluated.
*/
function mandatory() {
throw new Error('Missing parameter');
}
function foo(mustBeProvided = mandatory()) {
return mustBeProvided;
}
Hàm mandotory() sẽ được chạy khi tham số mustBeProvided bị thiếu.
> foo()
Error: Missing parameter
> foo(123)
123
Tham khảo:
- Sect. “Required parameters” in “Exploring ES6”
Tôi là Duyệt