Perfection Kills Quiz (part 14)
Previous posts on Perfection Kills JavaScript quiz: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13.
Finally, we've reached the last question of the Perfection Kills Quiz.
14)
with (function(x, undefined){}) length;
Correct answer: 2.
To answer this question, it is helpful to look at the definition of with statement. A with statement consists of an expression (in the parentheses) and a statement or a block of statements. The expression is added to the scope chain used when evaluating the statement.
To get a better feel of what a with statement is, we can play with a couple of examples in the console.
Example 1:
a; //throws an error "a is not defined"
//as the property "a" is not defined in the global scope;
Example 2:
with ({a: "b"}) a; //evaluates to "b"
In the quiz question, the expression is function(x, undefined){}
. The statement is length
. The snippet of code evaluates to the value of property "length" on the object function(x, undefined){}
. Function objects' length property specifies the number of arguments expected by the function. The function function(x, undefined){}
expects two arguments. Hence, its length property has the value 2.
Note that undefined is just a name of the function's second parameter rather than the value undefined.
All posts on Perfection Kills JavaScript quiz: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14.