JavaScript Quiz Set(blog.bolshchikov.net)
blog.bolshchikov.net
JavaScript Quiz Set
http://blog.bolshchikov.net/post/40917260776/javascript-quiz-set
15 comments
Global variables are in fact properties of the global object. In web pages the global object is window, so you can set and access global variables using the window.variable syntax.
https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Va...
I guess some of this knowledge is helpful or necessary for people who want to know JS inside-out. Nevertheless, none of that code looked like anything a programmer would write if they respected readability and conveyance of code. Maybe I'm just taking things too seriously here...
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.)
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.)
The above example is at least academically interesting since you can reason through it with a bit of programming experience and intuition about operator precedence.
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.You also need to know how tokens are produced by lexical analysis. It could have produced:
'num1' '+' '++' 'num2'
In fact it produces 'num1' '++' '+' 'num2'
so it's not about evaluate from left-to-right.Is there a case in JavaScript where operators with the same precedence are not evaluated left-to-right?
I don't know. But I happen to know that the javascript tokenizer matches the longest possible substring. Therefore it takes '++' from '+++..', not '+'. It's not about precedence.
EDIT: Yes, the assignment operators, =, += etc., evaluates right to left.
EDIT: Yes, the assignment operators, =, += etc., evaluates right to left.
[deleted]
[SPOILERS]
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.
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.
From the answers to the first intermediate quiz:
That said, these quizzes are a good way to learn but man does JavaScript have a lot of traps.
// 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.
Second example, "function scope"
"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.
"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.
No, you're incorrect, C and Java do have block scope. It would have taken like 5 seconds to verify that. In C:
#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)Original author of that quiz here.
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_...
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_...
The third question - "Global Variables Intro" in the "beginner" (one by http://madebyknight.com/javascript-scope/) is not quite right.
is not the same as
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 -
But you can't delete 'a' if it were defined by var.
In the same light, the following statement (IMO) is incorrect.
Still working through the tutorial ...
Edit - Copy pasted the wrong sentence from the quiz. Sorry