Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'ll admit it, I haven\'t yet mastered the language, but my experience with it

ID: 646834 • Letter: I

Question

I'll admit it, I haven't yet mastered the language, but my experience with it tells me that Javascript is a highly flexible language, allowing prototypal inheritance, dynamic typing, functions as first class citizens and so many more cool stuff. I think such features bring some inconsistencies, but that's nothing new, most people would agree with me. I wanted to discuss specific examples that have been annoying me, like "for in" loops:

Why should I need to do this? Doesn't this break the concept of classes and inheritance? I might not want to loop through the whole prototype, but what if I still need some of the attributes/methods from an object's direct parent, for example?

Also, I would be most grateful if someone could explain me why the interpreter doesn't allow synonymous global/local variables to share the same scope, when there is certainly a way it could distinguish one from the other. Like in:

You can see that, even though our local bar has been declared after the call to our global bar, bar's value in foo's scope will always reflect the local bar, which will hold 'undefined'. Isn't that a strange behavior? I would love if senior Js programmers enlightened me on this and showed me what is gained from these seemingly strange features and if the disadvantages I mentioned here are valid.

Explanation / Answer

Why should I need to this? Doesn't this break the concept of classes and inheritance? I might not want to loop through the whole prototype, but what if still need some of the attributes/methods from an object's direct parent, for example?

JavaScript is essentially a functional language that has imperative style. JavaScript can fake classes and inheritance, but they aren't well-supported first class concepts. As such, you'll naturally hit some walls when you want to express ideas that way. There are some good treatments of JavaScript as OOP around the Internet. Chances are good that if you're hitting your head against a wall with JavaScript, somebody else before you did as well.

Also, I would be most grateful if someone could explain me why the interpreter doesn't allow synonymous global/local variables to share the same scope, when there is certainly a way he could distinguish one from the other.

The dynamic type system means that the interpreter cannot say what the type of bar is. As a bonus side note, JavaScript also performs hoisting automatically.

When I executed your example code, I got a complaint because foo is an immediately invoked function expression, so bar isn't defined at that point.

TypeError: bar is not a function
var wth = bar();
But suppose that wasn't the case. When you assign wth to bar(), it sees that you're attempting to invoke bar as a callable. function bar() is almost syntactic sugar for var bar = function(). Note the link in Neil's comment for why it's "almost."

The type system cannot differentiate between var bar = 5; and var bar = function() { return 5; } because you can do evil things like the following:

var i = 5;
console.log(typeof i);
i = 'hello';
console.log(typeof i);
i = function() { return 'f'; }
console.log(typeof i);
console.log(typeof i());
The output will be:

number
string
function
string
This is what I mean by the dynamic type system cannot say what the type of bar is; a variable just holds data and the data has a type. So if you try to do something like:

var i = 5;
console.log(i());
You'll end up with a type error because i isn't a callable and you're trying to use it as one.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote