Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Zakas' new Quiz

Reply
Thread Tools

Zakas' new Quiz

 
 
Garrett Smith
Guest
Posts: n/a
 
      02-19-2010
Yet another JS Quiz:
http://www.nczonline.net/blog/2010/0...vascript-quiz/

I have a couple of comments on the explanations provided:
http://www.nczonline.net/blog/2010/0...-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/ar...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/
 
Reply With Quote
 
 
 
 
Asen Bozhilov
Guest
Posts: n/a
 
      02-19-2010
Garrett Smith wrote:
> Yet another JS Quiz:http://www.nczonline.net/blog/2010/0...vascript-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.
 
Reply With Quote
 
 
 
 
Garrett Smith
Guest
Posts: n/a
 
      02-20-2010
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/
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-20-2010
Garrett Smith wrote:

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


The DMDScript source code in D indicates that it implements
String.prototype.substr():

<http://www.digitalmars.com/dscript/>


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
Reply With Quote
 
Asen Bozhilov
Guest
Posts: n/a
 
      02-20-2010
Richard Cornford wrote:
> Asen Bozhilov wrote:


> > I was give answer TypeError on this question.

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


Exactly. `String.prototype.substr' isn't part from any editions of
ECMA-262 standard. On this questions `TypeError' is possible answer.
Another possible answer is: "I don't know." and again is correct
answer.

| 2 Conformance
| [...]
| In particular, a conforming implementation of
| ECMAScript is permitted to provide properties not described in this
specification,
| and values for those properties,
| for objects that are described in this specification.

The standard permit methods like `substr', but these methods are
implementation depended, because concrete implementation can implement
in own way. I don't think methods like `substr' should be part from
quizzes, because confuse readers, especially when author of quiz isn't
specified concrete environment.




 
Reply With Quote
 
Asen Bozhilov
Guest
Posts: n/a
 
      02-20-2010
Richard Cornford wrote:

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


How can you explain:

var x = 10;
+++x;

There SyntaxError instead of runtime error.

 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      02-20-2010
On Feb 19, 10:38*pm, Garrett Smith <dhtmlkitc...@gmail.com> wrote:
>
> 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]].


Yep, every function carries its context with it, that's what closures
are about, and as soon as a reference to a function survives longer
than the context in which it was created (as when saved in the
setTimeout queue), a closure is born.

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


No. Not at all. It's explained by the fact that "this" is === window.
It has nothing to do with the execution context. It could have been
very well any other context !== the global one, and still "this" would
have been "window". E.g. if you wrapped it into yet another function.
--
Jorge.
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      02-20-2010
On Feb 20, 10:21*am, Asen Bozhilov <asen.bozhi...@gmail.com> wrote:
> Richard Cornford wrote:
> > 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.

>
> How can you explain:
>
> var x = 10;
> +++x;
>
> There SyntaxError instead of runtime error.


As Richard said, the parser tries to math the longest sequence of
characters that makes sense. In this case ++ makes sense, but as soon
as the next character found is +, it stops making sense and throws.
--
Jorge.
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      02-20-2010
On Feb 20, 10:46*am, Jorge <jo...@jorgechamorro.com> wrote:
>
> As Richard said, the parser tries to math the longest sequence of
> characters that makes sense. In this case ++ makes sense, but as soon
> as the next character found is +, it stops making sense and throws.


s/math/match/
--
Jorge.
 
Reply With Quote
 
Asen Bozhilov
Guest
Posts: n/a
 
      02-20-2010
Jorge wrote:
> Asen Bozhilov wrote:
> > Richard Cornford wrote:


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

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

> > There SyntaxError instead of runtime error.


> As Richard said, the parser tries to math the longest sequence of
> characters that makes sense. In this case ++ makes sense, but as soon
> as the next character found is +, it stops making sense and throws.


From which grammar rules `+y` isn't valid `UnaryExpression`?

And why in next example there runtime exception instead of
SyntaxError?

++ function(){}(); //ReferenceError
++ function(){}; //SyntaxError

`++` operator works with ReferenceType "13.2.1 [[Call]]", never return
ReferenceType, which is logical because if [[Call]] return
ReferenceType will be have reference to Activation Object of execution
context which is finish. And AO cannot be marked as garbage collection
when execution context exit.





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[QUIZ] Gathering Ruby Quiz 2 Data (#189) Daniel Moore Ruby 10 01-31-2009 08:36 PM
[QUIZ] Newbie doubts about the quiz Marcelo Alvim Ruby 15 08-16-2006 02:08 PM
[QUIZ] 1-800-THE-QUIZ (#20) Ruby Quiz Ruby 15 02-24-2005 06:05 AM
[SOLUTION] Ruby Quiz #15 Animal Quiz David Tran Ruby 9 01-21-2005 02:11 AM
[QUIZ] Animal Quiz (#15) Ruby Quiz Ruby 11 01-18-2005 02:42 PM



Advertisments