5-part JS quiz (link)
Monday, January 18, 2010 6:44:38 PM
Rather than just linking to it I'll explain all the examples and why exactly you get the result you do ... but not just yet
I'll first let my dear readers dive in and take the quiz. Explanations will follow in comments (unless you all beat me to it, of course).








João EirasxErath # Monday, January 18, 2010 7:31:05 PM
lucideer # Monday, January 18, 2010 7:51:29 PM
Other than that I failed 1, 3 and 5
... and can't figure out why I got 5 wrong
(3 I understand, and 1 I just guessed wildly anyway)
Anonymous # Monday, January 18, 2010 8:37:36 PM
lucideer # Monday, January 18, 2010 8:58:38 PM
Originally posted by anonymous:
Aha.. ok. I actually thought it was the other way around (arguments object elements are pointers) but hey - thanks for the explanation.
Anonymous # Monday, January 18, 2010 10:33:20 PM
lucideer # Monday, January 18, 2010 11:04:02 PM
I would say what is demonstrated in at least three of those tests is potentially useful to know for any javascript developer - it's not just art for art's sake, it's testing the extent of your understanding of how javascript operates.
Kyle Bakerkyleabaker # Monday, January 18, 2010 11:41:59 PM
I did about the same as you. I couldn't really figure out how another answer could be confused on #4 either until explained above. #2 and 3 were the ones that got me, but those are probably just as easy for others. Cool post, thanks for sharing Hallvord!
Robin ZalekBtEO # Monday, January 18, 2010 11:42:20 PM
Michael A. Puls IIburnout426 # Tuesday, January 19, 2010 12:16:27 AM
Got the 5th one wrong, but I should no better on that. I'll blame that one on a memory lapse.
Got the rest right. But, getting the 4th one right was probably due to your warning and due to just seeing the topic at hand on some list or bug report the other day.
lucideer # Tuesday, January 19, 2010 2:30:59 AM
Michael A. Puls IIburnout426 # Tuesday, January 19, 2010 2:53:59 AM
Originally posted by lucideer:
See 15.3.4.3, 15.3.4.4: on page 248 of the 5th edition spec for some talk about this. There may be other sections too.
lucideer # Tuesday, January 19, 2010 3:27:09 AM
Daned4n3 # Tuesday, January 19, 2010 4:18:46 AM
#4 seems a bit evil, since you don't really see pointers in javascript. My first thought was that it was a trick question and that a is passed by value (so the call to arguments[2] shouldn't have an effect on a), so this one really surprised me.
_Grey_ # Tuesday, January 19, 2010 5:42:05 AM
#1 was a gotcha. I think it would be against best practice to even try such a thing though. My fleeting analysis says variable declarations are completely ignored in conditional statements.
#3 kind-of makes sense if you think of it as purely "variable declaration" instead of "variable declaration and instantiation". It works the same way in other languages. Some of those languages don't allow the use of uninstantiated variables, though.
In short: the expressions
var a;andvar a = undefined;are not equivalent.#2 is about the difference between function declarations and function expressions. Function declarations can't appear on the right side of an equal sign. IE (used to?) to have a bug where this works differently.
#4 is about the arguments object, which works like an array that provides shortcuts to named function parameters (and all unnamed arguments). Everyone who's used the arguments object to get at things passed to the function knows about this.
#5 is about the well-known methods to create references to the window object (where the global context is used as a default object to pass).
Anonymous # Tuesday, January 19, 2010 6:17:50 AM
Anonymous # Tuesday, January 19, 2010 6:24:24 AM
lucideer # Tuesday, January 19, 2010 6:39:45 AM
Yes, I was aware of this. Although, does anyone know if 'use strict' functions at all in any interpreters yet?
sirnh1 # Tuesday, January 19, 2010 7:51:48 AM
johnnysaucepn # Tuesday, January 19, 2010 9:28:31 AM
Mark 'Tarquin' Wilton-Jonestarquinwj # Tuesday, January 19, 2010 10:06:31 AM
#1 The variable declaration, like all variable declarations, is secretly moved to the start of the current scope. As a result, it behaves as if the first line of that script was "var a;" and only the assignment "a = 1;" happens inside the conditional. The same then happens with function declarations (in browsers that support ECMAScript 3, not Gecko's "JavaScript 1.5" which only moves them to the start of the nearest conditional). #3 is just another demonstration of that effect.
#2 relates to how a function name does *not* become a global variable during an assignment. It can, however, be used inside the function code as a reference to the function (and yes _Grey_, IE has some bugs there).
#3 then checks that variable instantiation happens before function instantiation, as mentioned under point #1, and that the resulting variable then becomes the function reference *if* and only *if* the variable has been instantiated without already assigning it a value. (I may have missed some of the rules here, but that seems to be correct.)
#4 demonstrates how the arguments object uses the index to lookup the relevant parameter name, then acts as a pointer to the local variable with that name (it's done internally with a setter function, I think, according to section 10.6). It's quite unusual for JavaScript to have pointers for primitive variable references, so I'm not surprised that like Hallvord, I forgot that part and got the answer wrong.
#5 .call() is run as a standard method. However, .call itself then runs the function as if it was a method of the first argument passed to the .call() method. From section 10.4.3; "if thisArg is null or undefined, set the ThisBinding to the global object". As a result, "this" ends up as a pointer to the global object, which in browsers is "window". It's sortof like the script engine does this:
Function.prototype.call = function (oThis) {
if( oThis === null || oThis === window.undefined ) {
oThis = window;
}
oThis.__temp__ = this;
oThis.__temp__(restOfArgumentsGoHere);
delete oThis.__temp__;
};
MyOpera team, please fix this!fearphage # Tuesday, January 19, 2010 12:47:01 PM
johnnysaucepn # Tuesday, January 19, 2010 1:15:34 PM
Originally posted by fearphage:
Yeah, but then the test was for Javascript - you can get away with anything when you call yours Jscript!
_Grey_ # Tuesday, January 19, 2010 3:19:41 PM
Omega JuniorOmegaJunior # Tuesday, January 19, 2010 10:19:39 PM
#2 I got wrong because I incorrectly assumed that function a would be created in the global scope, instead of not at all.
#5 I got wrong because I expected 'this' to reflect the function itself, rather than a global scoped object (window, apparently).
As far as I remember, I've always had problems with 'this', unless used inside an object function.
FransFrenzie # Wednesday, January 20, 2010 11:01:34 AM
I failed 1. Does the ("a" in window) part secretly declare it in some internal scope?
Passed 2. This one seemed the easiest to me, although before I failed 1 I would've thought 1 and 2 were the easiest.
Failed 3. Very interesting, I did not know this was possible. I would've expected it to just say something like [function object].
Passed 4. I have never seen this arguments stuff before, but I figured it had to be meaningful because Hallvord said he failed. Turns out I interpreted that one right. I think I would've failed it otherwise.
Failed 5. First I thought it would refer to the scoped object (like window), but the whole .call(null) part means nothing to me (is that bad?) so I assumed it was meant to modify it to refer to the function itself or something along those lines. Something I wouldn't expect, at any rate. Turns out the result is the same as just calling a(); which in retrospect seems logical. Why would you use call without anything in it though?
Anyway, no need to reply to anything I said specifically if it was already addressed in the preceding comments, like I said, I managed to avoid reading any except the first.
[edit]
Reading other comments now.
Originally posted by johnnysaucepn:
I originally learned "javascript" in the form of Jscript. I may still have some leftover bugs in my head.
Originally posted by _Grey_:
While I tend to declare all variables I use in a function at the top of that function (a habit I've got from Java, but I do think it makes things easier too), that's certainly not true. There's definitely something special about this particular issue. Tarquin's explanation does seem to make a lot of sense.
[/edit]
Anonymous # Wednesday, January 20, 2010 11:57:53 PM
Anonymous # Thursday, January 21, 2010 12:08:19 AM
Anonymous # Thursday, January 21, 2010 12:09:16 AM
lucideer # Thursday, January 21, 2010 1:06:13 AM
Originally posted by anonymous:
Great. Are these changes tracked by any particular BTS entries?
Originally posted by anonymous:
It does if you register
Michael A. Puls IIburnout426 # Thursday, January 21, 2010 4:38:51 AM
Originally posted by lucideer:
https://bugzilla.mozilla.org/show_bug.cgi?id=482298 and the es5 bug it blocks may be relevant. There might be some more specific reports somewhere though.
Aux # Saturday, January 23, 2010 2:26:10 AM
Jeff WaldenWaldo # Monday, January 25, 2010 7:04:28 AM
Anonymous # Friday, January 29, 2010 6:45:36 PM
Hallvord R. M. Steenhallvors # Monday, February 1, 2010 5:17:53 PM