G
Garrett Smith
Yet another JS Quiz:
http://www.nczonline.net/blog/2010/02/16/my-javascript-quiz/
I have a couple of comments on the explanations provided:
http://www.nczonline.net/blog/2010/02/18/my-javascript-quiz-answers/
Example #1:
I believe the explanation is that the postfix is matched, as in:
TOKENS: num1, ++, +, num2;
The ++ operator must apply to num1.
Example #2:
The explanation provided mentions scope, and that is totally wrong. The
scope of the anonymous function expression wrapped in the setTimeout is
(anonymous)[[Scope]] --> doIt [[Scope]] -- global [[Scope]].
The answer as to why `this.x` === 5 is explained by the fact that the
execution context for the nonstandard `setTimeout` method is global context.
The global object and the global variable object are the same object,
`this` is global object and `this.x` is the global object's variable `x`
is resolved .
This actually brings upon another issue related to IE, where the global
object is not the global variable object, but is instead a Host object,
as explained many years ago.
http://blogs.msdn.com/ericlippert/archive/2005/05/04/414684.aspx
Example #4 is correct explanation but instead of using the non-normative
(nonstandard) `String.prototype.substr`, it would have been nice to see
`String.prototype.slice` used instead. At least it it would be good to
comment on `String.prototype.substr` being nonstandard, and point to the
divergences in how JScript deviates from Ecma-262 recommendation.
http://www.nczonline.net/blog/2010/02/16/my-javascript-quiz/
I have a couple of comments on the explanations provided:
http://www.nczonline.net/blog/2010/02/18/my-javascript-quiz-answers/
Example #1:
I believe the explanation is that the postfix is matched, as in:
TOKENS: num1, ++, +, num2;
The ++ operator must apply to num1.
Example #2:
The explanation provided mentions scope, and that is totally wrong. The
scope of the anonymous function expression wrapped in the setTimeout is
(anonymous)[[Scope]] --> doIt [[Scope]] -- global [[Scope]].
The answer as to why `this.x` === 5 is explained by the fact that the
execution context for the nonstandard `setTimeout` method is global context.
The global object and the global variable object are the same object,
`this` is global object and `this.x` is the global object's variable `x`
is resolved .
This actually brings upon another issue related to IE, where the global
object is not the global variable object, but is instead a Host object,
as explained many years ago.
http://blogs.msdn.com/ericlippert/archive/2005/05/04/414684.aspx
Example #4 is correct explanation but instead of using the non-normative
(nonstandard) `String.prototype.substr`, it would have been nice to see
`String.prototype.slice` used instead. At least it it would be good to
comment on `String.prototype.substr` being nonstandard, and point to the
divergences in how JScript deviates from Ecma-262 recommendation.