2 ms·
> typeof on array or on function on some implementations return Object, on some other something different, you often have to check for type of argument You sho
by masklinn 15y ago
> typeof on array or on function on some implementations return Object, on some other something different, you often have to check for type of argument
You should not be using `typeof` on object types (and this includes functions) anyway. Use `instanceof`.
> and properly doing this is pain (checking is something is array is often done by checking if it has "length" property!).
This really checks if something is array-like (for instance the jQuery object would pass this "test", even though it's not an array. Likewise for `arguments` or a NodeList)
- ajuc 15y agoMaybe it's me not understanding dynamic typing etc, but it makes me afraid someon will use my code with object like that: var rope = { color: "red", length: 10 };
- masklinn 15y agoThere's nothing useful you can do against that kind of stuff, so I'd recommend you just document your API correctly and if people want to be stupid, them's the break. I mean nothing stops a java developer from implementing a List throwing some kind of NotImplementedException everywhere (in fact, using `java.util.Collections.unmodifiableList` would be sufficient as that's exactly what it does: throw UnsupportedOperationException on every access to a mutation method, so it's going to blow up any time it's passed to an API which wants to modify the list it's given)