JavaScript Quiz Set
blog.bolshchikov.net
blog.bolshchikov.net
The third question - "Global Variables Intro" in the "beginner" (one by http://madebyknight.com/javascript-scope/) is not quite right.
function() {
a = 3;
alert(a);
}
is not the same as var a;
function() {
a = 3;
alert(a);
}
Whenever you don't declare a variable with a var, it does not become a global variable, rather, it becomes a property of the global (or host) object.The main difference here is that you can delete a property -
delete window.a;
But you can't delete 'a' if it were defined by var.In the same light, the following statement (IMO) is incorrect.
When Javascript comes across a global variable, it needs somewhere to put it. It needs somewhere that everywhere else on the page can access. So, it uses the window object.
Still working through the tutorial ...Edit - Copy pasted the wrong sentence from the quiz. Sorry
A code snippet like the following wouldn't make me ask how it works, but it would make me want to find the person responsible for putting this snippet in production code:
var num1 = 5, num2 = 10, result = num1+++num2;
(I'm probably thinking in this manner these days because I'm currently in a situation where I'm maintaining someone else's code instead of building something.)
Does plus or pre/postfix increment have higher precedence?
Probably increment.
Does prefix or postfix have higher precedence?
They're probably the same -> evaluate from left-to-right.
Code is equivalent to num1++ + num2;
But the examples like "what is the difference between substring() and substr()", "What is the lowest cross-browser increment that [setInterval()/setTimeout()] can accurately use" is just trivia and is boring. 'num1' '+' '++' 'num2'
In fact it produces 'num1' '++' '+' 'num2'
so it's not about evaluate from left-to-right.EDIT: Yes, the assignment operators, =, += etc., evaluates right to left.
I got tripped up by question #5 of one of the expert quizzes [1] due to a difference between ActionScript 3 and JavaScript [2].
In AS3, `(a).b` is equivalent to JS's `(null, a).b`. That is, the `this` binding is lost in AS3 (but not JS). I thus thought the anser was "20, 10, 10, 10", and not "20, 20, 10, 10".
AS3 is not a superset of ES3 as most people believe.
[1] http://dmitrysoshnikov.com/ecmascript/the-quiz/
[2] I have been working on an AS3 compiler lately, which is why I mixed JS and AS3.
// These are functions
// used as closures...
// This is good
(function() {
var foo = 'bar';
})();
This, to me, seems an odd use of the word closure. Since no value is "closed over" with a function in the internal scope and preserved, it is simply an anonymous function being immediately invoked.That said, these quizzes are a good way to learn but man does JavaScript have a lot of traps.
"Based on knowledge of several other languages (such as Java or C), this would certainly be true. However, Javascript doesn't change scope when entering if statements, loops, or anything like that, really."
Dude what the hell?
In Java and C, 'if' blocks don't have their own isolated scope either. The equivalent Java or C code would also work perfectly fine.
#include <stdio.h>
int main() {
if (1) {
int a = 5;
}
printf("%d\n", a);
return 0;
}
==> test.c:6: error: ‘a’ undeclared (first use in this function)As dhconnelly pointed out, Java and C definitely do have their own isolated scope for 'if' blocks. More information: http://en.wikipedia.org/wiki/Scope_(computer_science)#Scope_...