News for hackers. Stuff that matters.
Re:JavaScript Quiz Set by raju on Saturday January 19, 2013 @04:28PM (#5084827)
Interesting. I just started this, and came back here to post this.

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

Re:JavaScript Quiz Set by bolshchikov on Sunday January 20, 2013 @02:03AM (#5086254)
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...
Re:JavaScript Quiz Set by skylan_q on Saturday January 19, 2013 @12:47PM (#5084043)
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.)

Re:JavaScript Quiz Set by prophetjohn on Saturday January 19, 2013 @01:50PM (#5084292)
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.
Re:JavaScript Quiz Set by jonsen on Saturday January 19, 2013 @02:31PM (#5084451)
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.
Re:JavaScript Quiz Set by prophetjohn on Saturday January 19, 2013 @03:22PM (#5084612)
Is there a case in JavaScript where operators with the same precedence are not evaluated left-to-right?
Re:JavaScript Quiz Set by jonsen on Saturday January 19, 2013 @03:52PM (#5084710)
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.

Re:JavaScript Quiz Set by strager on Saturday January 19, 2013 @12:02PM (#5083896)
[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.

Re:JavaScript Quiz Set by minikomi on Saturday January 19, 2013 @07:27PM (#5085426)
From the answers to the first intermediate quiz:

    // 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.

Re:JavaScript Quiz Set by borplk on Saturday January 19, 2013 @08:37AM (#5083334)
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.

Re:JavaScript Quiz Set by dhconnelly on Saturday January 19, 2013 @09:13AM (#5083401)
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)
Re:JavaScript Quiz Set by niggler on Saturday January 19, 2013 @10:44AM (#5083673)
In older versions of C that was the only way to declare variables in a function after the first non-declaration statement.
Re:JavaScript Quiz Set by borplk on Saturday January 19, 2013 @09:38AM (#5083469)
thanks for correction
Re:JavaScript Quiz Set by Serplat on Saturday January 19, 2013 @11:44AM (#5083837)
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_...