Is there an option to not create an object with particular condition within constructor, e.g.
function Monster(name, hp) {
if (hp < 1) {
delete this;
}
else {
this.name = name;
}
}
var theMonster = new Monster("Sulley", -5); // undefined
I think what you're supposed to do is throw an exception.
function Monster(name, hp) {
if (hp < 1) {
throw "health points cannot be less than 1";
}
this.hp = hp;
this.name = name;
}
var m = new Monster("Not a good monster", 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With