I have javascript class like this
class Student {
constructor(name, age) {}
}
How to rise or return an error message when one of paramaters (lets say 'name') not being passed. something like :
if (!name) {
return "whoops, you forget a name"
}
I have javascript class like this
class Student {
constructor(name, age) {}
}
How to rise or return an error message when one of paramaters (lets say 'name') not being passed. something like :
if (!name) {
return "whoops, you forget a name"
}
The expression throwIfMissing() specifying the default value is evaluated, if the passed value is missing.
function throwIfMissing() {
throw new Error('Missing parameter');
}
class Student{
constructor(mustBeProvided = throwIfMissing()) {
return mustBeProvided;
}
}
var student = new Student(100); //works
console.log(student);
var err = new Student(); // throws Uncaught Error: Missing parameter
Look at the arguments object and see how many parameters it has. It is not correct just to test to see if its value is undefined as it may have been set to undefined.