From: Garrett Smith on
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.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Asen Bozhilov on
Garrett Smith wrote:
> Yet another JS Quiz:http://www.nczonline.net/blog/2010/02/16/my-javascript-quiz/

I saw this, but unfortunately seems my comment been moderate. However,
i cannot pass test for which author, doesn't specified concrete
environment, in which i can observe correct behavior.

If i see in practice `num1+++num`, I'll just stop to read this code,
because author doesn't give damn about readers of their code and
people which maintain this.

> Example #2:
> (anonymous)[[Scope]] --> doIt [[Scope]] -- global [[Scope]].

You are correct, but Global Object doesn't have internal [[Scope]]
property.

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

But what is matter calling execution context, when we talk about
`this` value in newly created execution context? I can't give answer
on this question, because `setTimeout` is host object. I don't know
how `setTimeout` invoke callback and what value pass for `this` value
on callback. This question is depend from caller, not from calling
execution context.


> Example #4

I was give answer TypeError on this question.
From: Richard Cornford on
Garrett Smith wrote:
> 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;

That is an explanation, but not a full explanation because it does not
state why +++ must be interpreted as the tokens ++ followed by the token
+, rather than + followed by ++. The answer to that is in the spec, in
the last sentence of the first paragraph of section 7, where it says
"The source text is scanned from left to right, repeatedly taking the
longest possible sequence of characters as the next input element". So
given +++, the longest possible sequence of characters that can be a
token is ++, leaving the last + to be the next token.

> The ++ operator must apply to num1.
>
> Example #2:
> The explanation provided mentions scope, and that is totally
> wrong.

Yes, it looks like a repetition of that running confusion between
javascript's scope and the value of the - this - keyword.

> The scope of the anonymous function expression wrapped in the
> setTimeout is (anonymous)[[Scope]] --> doIt [[Scope]] --
> global [[Scope]].
<snip>

And is completely irrelevant to the resolution of the - this - keyword.

Richard.

From: Richard Cornford on
Asen Bozhilov wrote:
> Garrett Smith wrote:
<snip>
>> 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.
>
> But what is matter calling execution context, when we talk
> about `this` value in newly created execution context? I can't
> give answer on this question, because `setTimeout` is host
> object. I don't know how `setTimeout` invoke callback and
> what value pass for `this` value on callback.

You don't know that, but you do know that in ECMAScript the internal
GetValue function is called on the result of evaluating each argument
expression, so that all the values in the internal list passed to -
setTimeout - when it is called have had GetValue applied to them and are
therefore _never_ a Reference type. So, as any function argument passed
to - setTimeout - is never passed as a Reference type any value used
by - setTimeout - as the - this - value when invoking that function
could never be derived from the 'base' object of any Reference type
passed in.

In ES3 in general, when a - this - value is not the global object it is
the 'base' object from a Reference type. The obvious exception being the
use of the - Function.prototype.call - method, but - setTimeout - would
need a good reason for applying that to its function argument.

> This question is depend from caller, not from calling
> execution context.
>
>> Example #4
>
> I was give answer TypeError on this question.

For a system that does not implement a - substr - method for strings?

Richard.

From: Garrett Smith on
Richard Cornford wrote:
> Asen Bozhilov wrote:
>> Garrett Smith wrote:

[...]

>>> Example #4
>>
>> I was give answer TypeError on this question.
>
> For a system that does not implement a - substr - method for strings?
>
DMD Script, maybe?
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/