Frage

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 it's not a loose 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.

War es hilfreich?

Lösung 3

It's not strict wrong, because javascript is dynamically typed. But if it happens, it could hide a wrong architecture of your function.

What happens in some cases?

In some case, JavaScript core returns null if there is no result. Is the case of .match function. In this case, null is something like "I've found nothing".

In other case it returns -1 (i.e. .indexOf). In this case it's appropriate because the return value should be an integer positive. -1 it's a way to tell us: "I've found nothing" or "I've not found a positive integer".

So why it returns in one case null and in other way -1 for tell us the same thing?

In the first case the match should return an Array (that's an Object). In the second case the indexOf should return an integer. So -1 is more appropriate than null, because null it's an Object and not an integer.

Why not undefined? because undefined is something of undefined, like: "I've found nothing, and I don't know how tell you!". undefined generally is used when the function should returns a String but this string is not found. Returns "" is not elegant because it tell us: "I've found the String, and the string is empty".

Another case could be a function that returns a Boolean but in one case is undefined. So when you ask: "It's true or false?", it seems to says: "Neither!".

A beautiful example is this function:

 isAlive(SchroedingersCat); // <--- undefined!!!

What if your function should be return a DOM element?

See what jQuery doing.

jQuery return always an Object. This object (instance of itself) could be empty or not.

var obj = $('#idElement') // <--- return always a jQuery object
if(obj.length == 1) {
     // do something
}

This is an excellent trick in order to permit us to do a chain without force to check every time if we found something.

Andere Tipps

This is not wrong, since Javascript is dynamically typed.

However, it is often considered as bad taste: makes the code less readable, and might make it less efficiently translatable by JIT techniques.

It's not at all wrong. Your code will execute its job successfully. But the thing is it's not considered a very good programming practice. Though there are no hard and fast rules and in many situations such a methodology may be required, it's advisable to avoid this approach as far as applicable.

Secondly, you can consider re-factoring your code, so that it does only one thing. My suggestion would be to separate both the 'if's into two different methods.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top