3 ms·
I dislike javascript as much as the next guy, but the example in the article is absurd: function DoSomething (foo) { x = false} var x = 2; DoSomething(x); v
by rilita 12y ago
I dislike javascript as much as the next guy, but the example in the article is absurd:
function DoSomething (foo) { x = false}
var x = 2;
DoSomething(x);
var y = x - 1;
If you change "foo" to "x" then this works as expected. Who writes code that looks like this anyway? Decent JS developers know that declarations elevate themselves, and they also would not write code that looks like this.
This is like taking a couple words out of the middle of a sentence and complaining because the meaning changing versus the whole overall sentence.
- jfaucett 12y agoThis example though perhaps not the best actually makes it pretty obvious that if you have immutable objects and variables that cannot change their type you already know that y will always be 1, since it is impossible for DoSomething to change what x is bound to. So the example does illustrate the authors point about being able to understand code without having to look at implementation details.
- rilita 12y agoThat can easily be explained by the following example, which is much more sensible: var a_number = 10; a_number = { blah: 10 }; var a_plus_two = a_number + 4; Better yet you can simply say "Javascript does not have strict typing" and point people at the wiki article on strict typing. This is clear and concise as an explanation. Showing confusing examples and saying "oh look this is horrible types can change" does not make the point stronger. The article uses a whole lot of words and examples to point out problems that professional JS developers are aware of and work hard to avoid. It also implies that such languages without strict typing are "unreasonable". It's not unreasonable. It's that way on purpose.
- tormeh 12y agoI've accidentally done this in Python, though I didn't know functions could do that. Wasn't fun debugging. It's like "We've made this super-easy language where you don't need a main function, but if you don't have one all kinds of unintentional shit will happen to your code"
- mannykannot 12y agoPeople don't write code like this on purpose (except as an example). The point, I think, is that it happens by accident, and usually buried within a much bigger body of code.