Is it wrong too return diffrent types in a javascript function?
Is it wrong to return two different types from a JavaScript function? This
is often the case in PHP. For instance PHP's strpos()-function returns an
integer telling you the position or false if not found. In Java this would
not be possible since its not a lose typed language and the return type is
defined with the function.
So would be be wrong to do the same in JavaScript?
E.g.
function num(x, y) {
if (x !== y) {return false;}
if (x < 5) {return 5;}
return x;
}
In above example we return an integer or boolean. But is it wrong? Would
it make the life of the JavaScript engine harder and perhaps force it to
de-optimize?
Or in another example, what if I have a function that creates an element
and tries to put it into the DOM then returns the newly created element
object. But if it can't find the parent element you send as an param it
return false.
Thanks!
No comments:
Post a Comment