A function called as a constructor (with the new operator) will always return an instance unless it explicitly returns an object. You can therefore return an empty object, and use the instanceof operator to check what came back:
function Monster(name, hp) {
if (hp < 1) {
return {};
}
else {
this.name = name;
}
}
var theMonster = new Monster("Sulley", -5);
console.log(theMonster instanceof Monster); // false
This behaviour is explained in the spec (13.2.2):
8. Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.
9. If Type(result) is Object then return result.
10. Return obj.
However, as others have pointed out, whether you should actually do this is questionable.
Monsterfunction? You cannot usetheMonsteranyway if you don´t want it to be an object. You could addthis.isMonster = (hp >= 1);to yourMonsterfunction.