4 ms·
As a Pythonista Javascript is really strange. Let's combine two arrays with a + b. Wait, why is it now a string? Why did it just create a undefined.jpg when I r
by ElongatedTowel 13y ago
As a Pythonista Javascript is really strange. Let's combine two arrays with a + b. Wait, why is it now a string? Why did it just create a undefined.jpg when I run filename + '.jpg'? Ah damn, I named it filepath. I guess I check if this object is empty by comparing it to false, works for arrays right? Except it doesn't and it doesn't work for arrays either because they might contain a 0.
The lesson is, always look for a method that does what you want.
- TazeTSchnitzel 13y agoThe undefined.jpg thing won't happen for variables in strict mode, it'll error. [].length is what you want for the second. IIRC [] isn't false in Python either.
- deleted 13y ago[deleted]
- gjm11 13y ago[] is false in Python; so are other empty aggregates like () and {}; collection types implemented in Python typically follow the same pattern. Thus: >>> if (): print "tuple" >>> if []: print "list" >>> if {}: print "dict" >>> if set(): print "set" >>> if "": print "string" >>> import collections >>> if collections.Counter(): print "counter" >>> if collections.deque(): print "deque" >>> (Observe that none of these prints anything.)
- TazeTSchnitzel 13y agoHuh, I recall [] not working. Interesting.
- kevin-brown 13y ago[] is false in Python, along with most empty types. http://ideone.com/ROMcag http://ideone.com/ROMcag
- mistercow 13y agoYou really wouldn't want an empty object to be equal to zero, since it might have a prototype that gives it functionality (and having the behavior conditional upon that would make it preposterously complicated to use correctly). The real mistake was making [] equal zero.
- TazeTSchnitzel 13y ago[] doesn't equal zero, but '' does, and [] is coerced to '' which is coerced to 0, IIRC.
- mistercow 13y agoWhat's really interesting thing is that all numeric coercions go through a string coercion, and that coercion doesn't (at least in Chrome and Firefox) have to even return a string. Presumably, the toString() method is run repeatedly until either a string or a number comes out. So a simple fraction type can be done like this: function Fraction(n, d) { this.n = n; this.d = d; } Fraction.prototype.toString = function() { return this.n / this.d; }; //Demo var x = new Fraction(1, 2); alert(x * 38); //19 alert(x + x); //1
- rdtsc 13y agoYou call it strange, I call it broken. Python has strong types, so does Erlang. Javascript has weak types and I don't like it. Whether one can avoid it or not is a different argument, as obviously there aren't that many choices on the browser, but I often argue it is a broken behavior, it annoys me every time I have to use Javascript.
- peterashford 13y agoI program JS as my day job. It's a broken POS. You can do cool things with it but nothing can disguise the fact that it's only popular due to an accident of history, not because it's an appropriate language for the tasks it's used for.