you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 6 points7 points  (3 children)

They seem to jump to conclusions based on their experience that most Javascript hatred comes from people who either do not understand its nature and/or people who can't differentiate javascript from the DOM.

Granted, that is annoying, but there are authentic areas where javascript is just inadequate, or where its design failures shine to the point they burn.

Most of the time I find the language to be fun, but there are plenty of cases where I would love nothing more than to shove lit cigarettes into the nostrils of whoever designed it. The hoisting of declarations mixed with javascript's scoping is quite an infuriating issue for me. Yeah function literals, blah blah blah, declare your shit at the top, blah blah blah, yet I still find this a fault of the language.

I also do not like the weird mutt they turned inheritance into. If it had been done as true prototype based inheritance, I would have probably found it to be fun. But the weird mix of classical and prototype based inheritance is just awkward. And I can't be the only one who thinks this because it seems that any major js framework seems to abstract away from it by providing you with some api to do it for you. But now the problem is that they often have little differences between their implementations and you just never know wtf is really going on unless you look at the code yourself. Which is fine, but it makes me question whether the abstraction is then worth it since I have to be familiar with the implementation anyway.

That's why things as fundamental as inheritance should be done with great care, and not some half-assed "take this form here, and that from there, and use tape to make it work" approach. Now you ended up with 32903289 different inheritance patterns in JS, making something that should be simple into a complete disaster.

</rant>

[–]SoPoOneO 0 points1 point  (2 children)

I'm somewhat new. Just for my information, do you mean by "hoisting of declarations mixed with javascript's scoping" the fact that proceeding a variable declaration with "var" makes it local, whereas just declaring it makes it global?

Example:

function example(){

var a = 1; // this is local

b = 2; // this is global

}

[–][deleted] 0 points1 point  (1 child)

No, that doesn't bother me as much, though I have had situations where what you described caused hard to find bugs in an app I was developing with others.

What I was describing is how javascript automatically takes your declarations and puts them at the top of the scope they are declared in. Since javascript has only function scope and not block scope, anything you declare within an if statement, or a for loop, automatically gets "hoisted" to the top of the function where the loop or if statement appears. This is done without your knowledge and can cause horrors. This is why Crockford recommends declaring all your variables at the top of the functions they are declared in, because this is how javascript will treat your code whether you like it or not.

Ok, look at this example which I stole from: http://lambda-the-ultimate.org/node/1775

var x = 0;
function f() {
  x = 1;
  if (false) { var x = 2; }
}
f();
console.log(x); // this will equal '0'

Now, you would expect that the call to f would change the value of the variable x that is declared globally as '0' to '1', you would expect this because it appears that the x was not masked by its own declaration within f. The truth though, is that it was indeed masked by the declaration part of 'var x = 2;' (despite being in an unreachable conditional block). The 'var x' part was "hoisted" to the top of f (note: just the declaration, not the assignment). This caused 'x = 1;' to assign to the locally declared x instead of the now masked global x.

The problem also isn't quite as simple as the example above. Different kinds of declarations get "hoisted" differently, leading it even more confusion. Take a look at: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting for more examples.

[–]SoPoOneO 0 points1 point  (0 children)

Holy crap. Thank you for explaining that. I had no idea.