> ... he pokes fun at some apparently crazy behaviours in Ruby and Javascript.<p>I am sorry, but that still is crazy behavior. You can excuse it with a historical perspective ("well it had to hide errors from users") or by explaining "the why" like you do, that still doesn't make that behavior less crazy.
This is not "WHY", this is "HOW exactly". I bet the reason why JavaScript has these behaviors is due it being developed as a browser scripting language. Authors didn't want the entire script terminating because some part of it was buggy. This could have been handled by some clever isolation and error-recovery mechanism, but it wasn't. Probably due to time constraints.<p>These kinds of things are one of the reasons why I really don't like the idea of everything (including server-side and standalone apps) being written in JavaScript.
This is only part of the why. The other part is the valueOf function which you can specify in order to control what the value of an object is when coerced.<p>So for instance:<p><pre><code> var arr1 = [1,2,3], arr2 = [1,2,3,4];
console.log(arr1 + arr2); // "1,2,31,2,3,4"
arr1.valueOf = function () { return this.length; };
arr2.valueOf = arr1.valueOf;
console.log(arr1 + arr2); // 7</code></pre>
text only cache, courtesy google: <a href="http://webcache.googleusercontent.com/search?q=cache:http://blog.caplin.com/2012/01/27/the-why-of-wat/&hl=en&strip=1" rel="nofollow">http://webcache.googleusercontent.com/search?q=cache:http://...</a><p>and a duckduckgo tip<p><pre><code> !cache http://blog.caplin.com/2012/01/27/the-why-of-wat/</code></pre>
The original presentation was nicely done, but knowing how things work in JavaScript (even though I don't know Ruby) meant I didn't exactly have that WTF-experience. This article explains it nicely to those who had.
Well i'v never understood the problems with JavaScript but maybe it's because i haven't done things how other languages do it. But js is very "open" and bound free. Especially with functions and objects. They make sense to someone who is new to OOP. maybe its not follow how things are usually done in other languages but that does not mean its necessarily a bad thing. If you think something doesn't make sense that does not mean its wrong. i am learning c and i have to say typed variables must be faster but they are difficult to gasp at first. In fact its a nightmare to make variables of a certain type, it limits my thought process when i have to think what the variable's type is after every line of code. Strings look like a huge deal. The fact that i have to predetermine the length of a string is laughable. Its my string and it can be as long as i want it to be anywhere in my code. (yes i now sound immature but this is my experience until now, call it a rant if you may) Alot of things need to be known before we can write a meaningful program. Thats not the case with js or even with php to some extent.
> It can be a prefix ‘this number is positive’ operator where it operates on a single number.<p>`+` in this context is the "Unary + Operator" which coerces a value to a number via the internal `toNumber` method of the operand.<p>It doesn't make the number positive:<p><pre><code> node> +-1 // -1</code></pre>
Automatic type coercion feels like a double-barreled shotgun, with one of the barrels pointed back at you:<p>0. Is only marginally useful
1. You have to be careful with what are you loading, and where
2. Will sooner or later blow up in your face
The syntax doesn't bug me too much. Most of it is documented fairly well. The craziest thing about JavaScript is a lack of any good online reference for their classes and methods. JavaScript documentation usually just resorts to a key example or a brief description without covering all the details.