Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JavaScript knowledge test

89 views
Skip to first unread message

marss

unread,
Jul 31, 2007, 8:24:48 AM7/31/07
to
Maybe anyone know good free online JavaScript knowledge test? This not
exactly a system for testing online required - it may be simply list
of questions with variants of answers (I have to prepare tests for
learners and I need something to be taken as basis).

I was able to find only this (http://www.w3schools.com/js/
js_quiz.asp), but I need more.

Thanks,
Mykola

rf

unread,
Jul 31, 2007, 8:57:42 AM7/31/07
to

"marss" <mars...@gmail.com> wrote in message
news:1185884688.7...@l70g2000hse.googlegroups.com...

> Maybe anyone know good free online JavaScript knowledge test? This not
> exactly a system for testing online required - it may be simply list
> of questions with variants of answers (I have to prepare tests for
> learners and I need something to be taken as basis).

Why?

> I was able to find only this (http://www.w3schools.com/js/

Do not even consider stuff from this source.

Their question 18: The best way to open a new window. Har har :-)

Henry

unread,
Jul 31, 2007, 9:02:05 AM7/31/07
to

If your need is real you need something else entirely that that
'test'. Almost none of the questions asked in that test include the
'correct' answer (by which I mostly mean optimum answer) in the list
of the choices offered. The worst, and most telling in its
erroneousness, is the question; "How many different kind of loops are
there in JavaScript", for which the multiple choice answers are:-

1. One. The "for" loop
2. Two. The "for" loop and the "while" loop
3. Four. The "for" loop, the "while" loop, the "do...while" loop, and
the "loop...until" loop

- none of which are close (even giving a lot of latitude for
interpretation (is for(;;) the same as for(in)?)).

marss

unread,
Jul 31, 2007, 9:46:32 AM7/31/07
to
On 31 , 15:57, "rf" <r...@invalid.com> wrote:
> > I was able to find only this (http://www.w3schools.com/js/
>
> Do not even consider stuff from this source.
>
> Their question 18: The best way to open a new window. Har har :-)
>


What is the correct JavaScript syntax for opening a new window called
"window2" ?
1.window.open("http://www.w3schools.com","window2")

To my shame, I can't see mistake here (not "best way" but ""correct
syntax") :(

marss

unread,
Jul 31, 2007, 9:47:03 AM7/31/07
to
On 31 , 16:02, Henry <rcornf...@raindrop.co.uk> wrote:

>
> 1. One. The "for" loop
> 2. Two. The "for" loop and the "while" loop
> 3. Four. The "for" loop, the "while" loop, the "do...while" loop, and
> the "loop...until" loop
>

:) I saw it. I am not going to copypaste without thinking over.

Any link for something more reliable will be appreciated.

Peter Michaux

unread,
Jul 31, 2007, 11:29:19 AM7/31/07
to

dhtmlk...@gmail.com

unread,
Jul 31, 2007, 6:56:41 PM7/31/07
to

Jeopardy style:

1. b is a built-in object; not a String or string literal. Define b.
if( b ) {
alert( "if: " + typeof b );
}
else {
alert( "else: " + b );
}
result: alerts "if: false"

2. Define p and q (they are not Strings). Result:
p < q; // false.
p <= q; // true.
p == q; // false.


Richard Cornford

unread,
Jul 31, 2007, 7:43:03 PM7/31/07
to
Peter Michaux wrote:

> On Jul 31, 5:24 am, marss wrote:
>> Maybe anyone know good free online JavaScript knowledge
>> test? This not exactly a system for testing online required
>> - it may be simply list of questions with variants of
>> answers (I have to prepare tests for learners and I need
>> something to be taken as basis).
>>
>> I was able to find only this (http://www.w3schools.com/js/
>> js_quiz.asp), but I need more.
>
> http://blog.meebo.com/?page_id=254

When the question of seeking javascript tests on the interment comes up
my imagination usually conjures up an individual who thinks they will be
able to get away with using other people's code and copy-pasting their
way though life, if only they could get a foot through the door.
Preferably into a job where they were the only person creating the
javascript, and so not have anyone looking over their shoulders that
knew what they were doing. The good thing about such a position is that
the people doing the interview would not know what questions they should
be asking and so would likely get any technical test they used off the
Internet themselves. And so a thorough search for such tests, and the
rote learning of the 'correct' answers, might get them past the
technical test and into such a job.

But then I am very cynical.

I have the problem of setting javascript technical tests for interviews
(not that often, but it is part of my job), so form time to time I think
about what questions they should include, and what I would be looking
for in an answer. For example, as a verbal question I would tend to ask;
"which side of an inequality expression is evaluated first?" Not because
I want to be told the answer (I would not memorise that sort of detail
(after all, it does not matter 99.9% of the time), and it would scare me
to encounter someone who did), but because I would want to hear "I would
have to look it up" (or words to that effect), so I could ask where they
would look it up, and if given the correct answer hand them a copy of
the document to see if they were familiar enough with it (and
interpreting it) to give me the correct answer quickly.

On the other hand there are things I would expect someone to know
(without having to look it up), and one of those is Identifier
resolution against the scope chain.

"Inspired" by one of the more ambiguous questions on your meebo.com page
I thought the following might make quite interesting written test
questions, and give an impression of my thought process in setting
javascript questions:-

/* unknown global code */
function outerFunction(){
/* unknown outer function body code */
function innerFunction(){
/* unknown inner function body code */
with(anObjectReference){
x = 5; //<--- The subject line of code.
}
/* more unknown inner function body code */
}
/* more unknown outer function body code */
}
/* more unknown global code */

/* ********************************************************\
| Note: Three facts about the 'unknown' code:- |
| |
| 1. There are no more function definitions, no function |
| expressions and no uses of the Function constructor. |
| 2. There are no - with - statements in the unknown code.|
| 3. There are no uses of the - eval - function. |
\******************************************************** */


Q1: Assuming the line that reads - x = 5; - is executed, which (group
of) of the following are possible outcomes of its execution?

1. The creation of an 'x' property of the 'outerFunction' function
and the assignment of the value 5 to that property.

2. The assignment of the value 5 to a pre-existing 'x' property of
the 'outerFunction' function.

3. The creation of an 'x' property of the 'innerFunction' function
and the assignment of the value 5 to that property.

4. The assignment of the value 5 to a pre-existing 'x' property of
the 'innerFunction' function.

5. The creation of an 'x' property of the object referred to by
'anObjectReference' and the assignment of the value 5 to that
property.

6. The assignment of the value 5 to a pre-existing 'x' property of
the object referred to by 'anObjectReference'.

7. The creation of a local variable of the 'outerFunction' function
named 'x' and the assignment of the value 5 to that variable.

8. The assignment of the value 5 to a declared local variable of the
'outerFunction' function named 'x'.

9. The creation of a local variable of the 'innerFunction' function
named 'x' and the assignment of the value 5 to that variable.

10. The assignment of the value 5 to a declared local variable of the
'innerFunction' function named 'x'.

11. The creation of a global variable named 'x' and the assignment of
the value 5 to that variable.

12. The assignment of the value 5 to a declared global variable
named 'x'.

13. The creation of an 'x' property of the global object and the
assignment of the value 5 to that property.

14. The assignment of the value 5 to a pre-existing 'x' property of
the global object.

15. The creation of an 'x' property of the window object and the
assignment of the value 5 to that property.

16. The assignment of the value 5 to a pre-existing 'x' property of
the window object.

17. A runtime error.

Q2: If the line of code above is changed from - x = 5; - to - var x =
5 - which (group of) the above are then the possible outcomes of the
execution of that line?

I would have to go over the answers with the candidate taking the test
as there are a number of 'understandable mistakes' to be easily made
here (that is, getting some of them wrong is a certain fail, but others
may need the thinking behind the answer.)

Richard.

David Mark

unread,
Jul 31, 2007, 7:53:28 PM7/31/07
to
On Jul 31, 6:56 pm, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
wrote:

> On Jul 31, 8:29 am, Peter Michaux <petermich...@gmail.com> wrote:
>
> > On Jul 31, 5:24 am, marss <marss...@gmail.com> wrote:
>
> > > Maybe anyone know good free online JavaScript knowledge test? This not
> > > exactly a system for testing online required - it may be simply list
> > > of questions with variants of answers (I have to prepare tests for
> > > learners and I need something to be taken as basis).
>
> > > I was able to find only this (http://www.w3schools.com/js/
> > > js_quiz.asp), but I need more.
>
> >http://blog.meebo.com/?page_id=254
>
> > Peter
>
> Jeopardy style:
>
> 1. b is a built-in object; not a String or string literal. Define b.
> if( b ) {
> alert( "if: " + typeof b );
> }
> else {
> alert( "else: " + b );
> }
> result: alerts "if: false"

I'll have to think about this one for a moment.

>
> 2. Define p and q (they are not Strings). Result:
> p < q; // false.
> p <= q; // true.
> p == q; // false.

p = 0;
q = null;

Richard Cornford

unread,
Jul 31, 2007, 8:21:00 PM7/31/07
to
dhtmlk...@gmail.com wrote:
> On Jul 31, 8:29 am, Peter Michaux wrote:

>> On Jul 31, 5:24 am, marss wrote:
>>
>>> Maybe anyone know good free online JavaScript knowledge
>>> test? This not exactly a system for testing online
>>> required - it may be simply list of questions with
>>> variants of answers (I have to prepare tests for
>>> learners and I need something to be taken as basis).
>>
>> > I was able to find only this (http://www.w3schools.com/js/
>> > js_quiz.asp), but I need more.
>>
>> http://blog.meebo.com/?page_id=254
>>
>> Peter
>
> Jeopardy style:
>
> 1. b is a built-in object; not a String or string literal.
> Define b.
> if( b ) {
> alert( "if: " + typeof b );
> }
> else {
> alert( "else: " + b );
> }
> result: alerts "if: false"

Given the definition of "built-in object" in ECMA 262, 3rd Ed. Section
4.3.7, and especially the words "Every built-in object is a native
object", and the definition of - typeof - operator in section 11.4.3,
where all 'native' objects must result in the strings 'object' or
'function' when used as a operand of - typeof -, are you sure you are
not testing for knowledge of implementation bugs here?

> 2. Define p and q (they are not Strings). Result:
> p < q; // false.
> p <= q; // true.
> p == q; // false.

What about the execution order of these tests, as a strict adherence to
the order shown greatly expands the possibilities?

Richard.

David Mark

unread,
Jul 31, 2007, 9:35:59 PM7/31/07
to
On Jul 31, 7:43 pm, "Richard Cornford" >

> /* unknown global code */
> function outerFunction(){
> /* unknown outer function body code */
> function innerFunction(){
> /* unknown inner function body code */
> with(anObjectReference){
> x = 5; //<--- The subject line of code.
> }
> /* more unknown inner function body code */
> }
> /* more unknown outer function body code */}
>
> /* more unknown global code */
>
> /* ********************************************************\
> | Note: Three facts about the 'unknown' code:- |
> | |
> | 1. There are no more function definitions, no function |
> | expressions and no uses of the Function constructor. |
> | 2. There are no - with - statements in the unknown code.|
> | 3. There are no uses of the - eval - function. |
> \******************************************************** */
>
> Q1: Assuming the line that reads - x = 5; - is executed, which (group
> of) of the following are possible outcomes of its execution?
>
> 1. The creation of an 'x' property of the 'outerFunction' function
> and the assignment of the value 5 to that property.

Dunno.

>
> 2. The assignment of the value 5 to a pre-existing 'x' property of
> the 'outerFunction' function.

function outerFunction() {

function innerFunction() {


with(anObjectReference){
x = 5; //<--- The subject line of code.
}
}


innerFunction()
}
outerFunction.x = 0
var anObjectReference = outerFunction;
outerFunction();


>
> 3. The creation of an 'x' property of the 'innerFunction' function
> and the assignment of the value 5 to that property.

Dunno.

>
> 4. The assignment of the value 5 to a pre-existing 'x' property of
> the 'innerFunction' function.

function outerFunction() {

function innerFunction() {


with(anObjectReference){
x = 5; //<--- The subject line of code.
}
}

innerFunction.x = 0
var anObjectReference = innerFunction;

innerFunction();

}
outerFunction();


>
> 5. The creation of an 'x' property of the object referred to by
> 'anObjectReference' and the assignment of the value 5 to that
> property.

Dunno.

>
> 6. The assignment of the value 5 to a pre-existing 'x' property of
> the object referred to by 'anObjectReference'.
>

function outerFunction() {

function innerFunction() {


with(anObjectReference){
x = 5; //<--- The subject line of code.
}
}

innerFunction();
}

var anObjectReference = new Object();
anObjectReference.x = 0;
outerFunction();


> 7. The creation of a local variable of the 'outerFunction' function
> named 'x' and the assignment of the value 5 to that variable.

Dunno.

>
> 8. The assignment of the value 5 to a declared local variable of the
> 'outerFunction' function named 'x'.


function outerFunction() {
var anObjectReference = new Object();
var x;
function innerFunction() {


with(anObjectReference){
x = 5; //<--- The subject line of code.
}
}

innerFunction();
}

outerFunction();

>
> 9. The creation of a local variable of the 'innerFunction' function
> named 'x' and the assignment of the value 5 to that variable.

Dunno.

>
> 10. The assignment of the value 5 to a declared local variable of the
> 'innerFunction' function named 'x'.

function outerFunction() {
var anObjectReference = new Object();

function innerFunction() {
var x;


with(anObjectReference){
x = 5; //<--- The subject line of code.
}
}

innerFunction();

}

outerFunction();

>
> 11. The creation of a global variable named 'x' and the assignment of
> the value 5 to that variable.

var anObjectReference = this;

function outerFunction(){

function innerFunction() {


with(anObjectReference){
x = 5; //<--- The subject line of code.
}

}
innerFunction()
}

outerFunction();

>
> 12. The assignment of the value 5 to a declared global variable
> named 'x'.

var x;
var anObjectReference = this;

function outerFunction(){

function innerFunction() {


with(anObjectReference){
x = 5; //<--- The subject line of code.
}

}
innerFunction()
}

outerFunction();


>
> 13. The creation of an 'x' property of the global object and the
> assignment of the value 5 to that property.

Same as 11.

>
> 14. The assignment of the value 5 to a pre-existing 'x' property of
> the global object.

Same as 12.

>
> 15. The creation of an 'x' property of the window object and the
> assignment of the value 5 to that property.

Same as 11.

>
> 16. The assignment of the value 5 to a pre-existing 'x' property of
> the window object.

Same as 12.

>
> 17. A runtime error.

var anObjectReference;

function outerFunction(){

function innerFunction() {


with(anObjectReference){
x = 5; //<--- The subject line of code.
}

}
innerFunction()
}

outerFunction();


>
> Q2: If the line of code above is changed from - x = 5; - to - var x =
> 5 - which (group of) the above are then the possible outcomes of the
> execution of that line?

9.

And perhaps that one was a trick question. I'm starting to wonder if
any of the "dunno" scenarios above are even possible.

Since I never use with clauses, this test would be a nightmare for
me. And yes, I verified some of my answers with alerts. All but a
couple were correct (and I corrected those that weren't.)

And aren't with clauses supposed to be taboo in JS?

>
> I would have to go over the answers with the candidate taking the test
> as there are a number of 'understandable mistakes' to be easily made
> here (that is, getting some of them wrong is a certain fail, but others
> may need the thinking behind the answer.)

I figure I failed, but this seems more of an academic exercise than a
practical test (and I am no expert on the guts of ECMAScript.) This
is reinforced by the fact that I haven't used a single with clause in
ten years of scripting Web pages/applications. I've exploited
closures once (thanks to a tip in one of your articles) and I never
nest functions in functions. I've never found a practical need to do
any of these things. Furthermore, using closures, nested functions,
etc. would seem a bad idea if you consider the people who have to
maintain the code in the future (most of whom will likely be the
incompetent clipboard jockeys you alluded to in your preface.)

David Mark

unread,
Jul 31, 2007, 9:48:10 PM7/31/07
to
On Jul 31, 8:21 pm, "Richard Cornford" > > 1. b is a built-in object;

not a String or string literal.

[snip]

> > Define b.
> > if( b ) {
> > alert( "if: " + typeof b );
> > }
> > else {
> > alert( "else: " + b );
> > }
> > result: alerts "if: false"
>
> Given the definition of "built-in object" in ECMA 262, 3rd Ed. Section
> 4.3.7, and especially the words "Every built-in object is a native
> object", and the definition of - typeof - operator in section 11.4.3,
> where all 'native' objects must result in the strings 'object' or
> 'function' when used as a operand of - typeof -, are you sure you are
> not testing for knowledge of implementation bugs here?

Seems like it. That would make it a silly question for a general
JavaScript quiz though. Perhaps it is a trick question and the answer
is "nothing at all."

>
> > 2. Define p and q (they are not Strings). Result:
> > p < q; // false.
> > p <= q; // true.
> > p == q; // false.
>
> What about the execution order of these tests, as a strict adherence to
> the order shown greatly expands the possibilities?
>

I don't understand what you mean by that. I assume the question meant
to define the values once and then evaluate them in whatever order.
Do you mean that you could re-assign p and q in between each test and
come up with virtually infinite possibilities?

Peter Michaux

unread,
Aug 1, 2007, 12:15:14 AM8/1/07
to
On Jul 31, 4:43 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
[snip]

> But then I am very cynical.

At least you know! :)

> I have the problem of setting javascript technical tests for interviews
> (not that often, but it is part of my job), so form time to time I think
> about what questions they should include, and what I would be looking
> for in an answer.

Given your apparent high standards and opinion of the general quality
of JavaScript programmers, how do these employee searches go for you?
Do you simply take the best applicant or do you wait for a
satisfactory applicant?

> For example, as a verbal question I would tend to ask;
> "which side of an inequality expression is evaluated first?" Not because
> I want to be told the answer (I would not memorise that sort of detail
> (after all, it does not matter 99.9% of the time), and it would scare me
> to encounter someone who did), but because I would want to hear "I would
> have to look it up" (or words to that effect), so I could ask where they
> would look it up, and if given the correct answer hand them a copy of
> the document to see if they were familiar enough with it (and
> interpreting it) to give me the correct answer quickly.

If I was feeling bold and relatively happy with my current job, I
would answer that my code should not depend on knowledge of which side
is evaluated first. That is information that likely anyone would have
to look up and results in code that is not quickly read or easily
maintained.

> On the other hand there are things I would expect someone to know
> (without having to look it up), and one of those is Identifier
> resolution against the scope chain.
>
> "Inspired" by one of the more ambiguous questions on your meebo.com page
> I thought the following might make quite interesting written test
> questions, and give an impression of my thought process in setting
> javascript questions:-
>
> /* unknown global code */
> function outerFunction(){
> /* unknown outer function body code */
> function innerFunction(){
> /* unknown inner function body code */
> with(anObjectReference){
> x = 5; //<--- The subject line of code.
> }
> /* more unknown inner function body code */
> }
> /* more unknown outer function body code */}
>
> /* more unknown global code */
>
> /* ********************************************************\
> | Note: Three facts about the 'unknown' code:- |
> | |
> | 1. There are no more function definitions, no function |
> | expressions and no uses of the Function constructor. |
> | 2. There are no - with - statements in the unknown code.|
> | 3. There are no uses of the - eval - function. |
> \******************************************************** */

David Mark posted nice answers. I really enjoyed this as an exercise.
I too have never used "with".

I agree with David's sentiment about using "with" as a weeding
technique during the interview process. I think good JavaScript
programmers likely don't use the "with" statement because it is hard
to maintain, prone to creating properties in the wrong place and
cannot be optimized (well?) by the compiler. These warnings about
"with" are plastered all over the place and a good JavaScript
programmer my have never bothered to use "with" before. The places
where I have encountered "with" have been in code that otherwise had
horrendously bad JavaScript and was written by programmers for whom
I've come to have low respect for many reasons.

If you ever feel like posting other interesting exercises please do!

Thanks,
Peter

Peter Michaux

unread,
Aug 1, 2007, 12:46:54 AM8/1/07
to
On Jul 31, 6:35 pm, David Mark <dmark.cins...@gmail.com> wrote:
> On Jul 31, 7:43 pm, "Richard Cornford" >

> > 17. A runtime error.
>
> var anObjectReference;
>
> function outerFunction(){
> function innerFunction() {
> with(anObjectReference){
> x = 5; //<--- The subject line of code.
> }
> }
> innerFunction()
> }
> outerFunction();

I believe the error here isn't coming from the subject line of code
but the line before it.

A runtime error could still occur due to the subject line of
JavaScript. Taking it to the absurd, I have no clue what the
programmer who embedded JavaScript in a particular application has
done with the global x property. It may be that setting the global x
property always results in a runtime error.

> I figure I failed, but this seems more of an academic exercise than a
> practical test (and I am no expert on the guts of ECMAScript.) This
> is reinforced by the fact that I haven't used a single with clause in
> ten years of scripting Web pages/applications. I've exploited
> closures once

Holy cow! What about setting scope for an event, XHR or timeout
callback with objects? Never anything like this...

var thisC;
setTimeout(function() {thisC.callback();}, 1000);

> (thanks to a tip in one of your articles) and I never
> nest functions in functions. I've never found a practical need to do
> any of these things. Furthermore, using closures, nested functions,
> etc. would seem a bad idea if you consider the people who have to
> maintain the code in the future (most of whom will likely be the
> incompetent clipboard jockeys you alluded to in your preface.)

Restraint is in order for sure but no closures or use of higher-order
functions seems like, at the very least, the joy of writing JavaScript
would be completely gone.

Peter

Randy Webb

unread,
Aug 1, 2007, 12:48:59 AM8/1/07
to
marss said the following on 7/31/2007 8:24 AM:

> Maybe anyone know good free online JavaScript knowledge test?

What is ECMAScript and how often do you refer to it while writing an
actual script?

Anybody that has read more than 2 of my posts with regards to ECMAScript
can pretty much figure out my answer to that question. And, how that
question might have relevance to the value of a person to me as a
programmer.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

David Mark

unread,
Aug 1, 2007, 1:31:18 AM8/1/07
to
On Aug 1, 12:15 am, Peter Michaux <petermich...@gmail.com> wrote:
[snip]

> David Mark posted nice answers. I really enjoyed this as an exercise.

Thanks Peter. Of course, I didn't read the instructions carefully or
I would have realized that my suspicions about the impossibility of
some of the outcomes were part of the design. I was trying to come up
with code to make each outcome a reality (and gave up on 1, 3, 5, 7
and 9), when I should have taken a cue from the instructions and
expected some of them were going to be impossible. Apparently this
was a true/false exam with an obvious numbering pattern, but I was
treating it like an essay test. In the intended context, all of my
answers to the false questions were wrong as they will certainly be
interpreted as "I don't know if this is true or false."

David Mark

unread,
Aug 1, 2007, 1:42:00 AM8/1/07
to
On Aug 1, 12:46 am, Peter Michaux <petermich...@gmail.com> wrote:
> On Jul 31, 6:35 pm, David Mark <dmark.cins...@gmail.com> wrote:
>
> > On Jul 31, 7:43 pm, "Richard Cornford" >
> > > 17. A runtime error.
>
> > var anObjectReference;
>
> > function outerFunction(){
> > function innerFunction() {
> > with(anObjectReference){
> > x = 5; //<--- The subject line of code.
> > }
> > }
> > innerFunction()
> > }
> > outerFunction();
>
> I believe the error here isn't coming from the subject line of code
> but the line before it.

Correct. That's one I missed due to not following instructions to the
letter.

>
> A runtime error could still occur due to the subject line of
> JavaScript. Taking it to the absurd, I have no clue what the
> programmer who embedded JavaScript in a particular application has
> done with the global x property. It may be that setting the global x
> property always results in a runtime error.

I don't quite follow you there.

>
> > I figure I failed, but this seems more of an academic exercise than a
> > practical test (and I am no expert on the guts of ECMAScript.) This
> > is reinforced by the fact that I haven't used a single with clause in
> > ten years of scripting Web pages/applications. I've exploited
> > closures once
>
> Holy cow! What about setting scope for an event, XHR or timeout
> callback with objects? Never anything like this...

Yes, I have done that last one recently (technically with
setInterval.) The other thing I use closures for is to associate DOM
events with objects, which is the tip I got from Richard's article on
the subject. But it should be mentioned that that particular
technique will cause a memory leak in IE unless you clean up after it
when the page unloads.

[snip]

> Restraint is in order for sure but no closures or use of higher-order
> functions seems like, at the very least, the joy of writing JavaScript
> would be completely gone.

I find no real joy in writing JavaScript or any other sort of code. I
do enjoy designing and building applications (somewhat), but the
actual coding is drudgery to me.

marss

unread,
Aug 1, 2007, 3:29:06 AM8/1/07
to
On 31 , 18:29, Peter Michaux <petermich...@gmail.com> wrote:
>
> http://blog.meebo.com/?page_id=254
>
> Peter

Thanks for link, Peter.

This is interesting but I can't use it :(. My task is to test
understanding of JavaScript fundamentals and skills to use it in real
life applications. I am not going to reveal concealed gurus :)

marss

unread,
Aug 1, 2007, 3:32:02 AM8/1/07
to
On 1 , 02:43, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:

> When the question of seeking javascript tests on the interment comes up
> my imagination usually conjures up an individual who thinks they will be
> able to get away with using other people's code and copy-pasting their
> way though life, if only they could get a foot through the door.
> Preferably into a job where they were the only person creating the
> javascript, and so not have anyone looking over their shoulders that
> knew what they were doing. The good thing about such a position is that
> the people doing the interview would not know what questions they should
> be asking and so would likely get any technical test they used off the
> Internet themselves. And so a thorough search for such tests, and the
> rote learning of the 'correct' answers, might get them past the
> technical test and into such a job.
>
> But then I am very cynical.
>

Yes, you are. And your accusation is unjust. I am not interviewer. My
task is not to make learner out a fool but to test comprehension of
studied materials. I will never use questions I could not answer
myself. I know JavaScript a little more than my co-workers so I need
to organize teaching. I collected materials to study and I have to
test how they learned it. And before I will start to "invent bicycle"
I asked an advice.

> /* unknown global code */
> function outerFunction(){
> /* unknown outer function body code */
> function innerFunction(){
> /* unknown inner function body code */
> with(anObjectReference){
> x = 5; //<--- The subject line of code.
> }
> /* more unknown inner function body code */
> }
> /* more unknown outer function body code */}
>
> /* more unknown global code */
>

If I were HR manager I most likely do not hire a person who writes
code in that manner. Not because I am afraid of people who know more
than me. Gurus come and go but code leaves. Practice shows that
unmaintable code worse than inefficient code.


RobG

unread,
Aug 1, 2007, 9:50:41 AM8/1/07
to
On Aug 1, 9:43 am, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
[...]

> "Inspired" by one of the more ambiguous questions on your meebo.com page
> I thought the following might make quite interesting written test
> questions, and give an impression of my thought process in setting
> javascript questions:-

It seems few others are prepared to attempt a response, so I will.
Fools rush in and all that...


> /* unknown global code */
> function outerFunction(){
> /* unknown outer function body code */
> function innerFunction(){
> /* unknown inner function body code */
> with(anObjectReference){
> x = 5; //<--- The subject line of code.
> }
> /* more unknown inner function body code */
> }
> /* more unknown outer function body code */}
>
> /* more unknown global code */
>
> /* ********************************************************\
> | Note: Three facts about the 'unknown' code:- |
> | |
> | 1. There are no more function definitions, no function |
> | expressions and no uses of the Function constructor. |
> | 2. There are no - with - statements in the unknown code.|
> | 3. There are no uses of the - eval - function. |
> \******************************************************** */
>
> Q1: Assuming the line that reads - x = 5; - is executed, which (group
> of) of the following are possible outcomes of its execution?

The argument passed to a with statement is first evaluated, then an
attempt is made to see if the result is an object reference. If so, it
is placed at the top of the scope chain. Identifier resolution of the
statement in question then proceeds against this augmented scope
chain.


> 1. The creation of an 'x' property of the 'outerFunction' function
> and the assignment of the value 5 to that property.

Not possible. If a property x is created anywhere by that line, it
will be on the global object.


> 2. The assignment of the value 5 to a pre-existing 'x' property of
> the 'outerFunction' function.

Possible, if anObjectReference does not have an x property.


> 3. The creation of an 'x' property of the 'innerFunction' function
> and the assignment of the value 5 to that property.

Not possible, see answer to 1.


> 4. The assignment of the value 5 to a pre-existing 'x' property of
> the 'innerFunction' function.

Not possible, the innerFunction function object might have an x
property, but it isn't on the scope chain. However, innerFunction's
execution object (which is on the scope chain) may have an x property
if one's been declared and that could be assigned a value.


> 5. The creation of an 'x' property of the object referred to by
> 'anObjectReference' and the assignment of the value 5 to that
> property.

Not possible, see 1.


> 6. The assignment of the value 5 to a pre-existing 'x' property of
> the object referred to by 'anObjectReference'.

Possible, the usual intended result.


> 7. The creation of a local variable of the 'outerFunction' function
> named 'x' and the assignment of the value 5 to that variable.

Not possible, see 1 again.


> 8. The assignment of the value 5 to a declared local variable of the
> 'outerFunction' function named 'x'.

Possible, since that is on the scope chain.


> 9. The creation of a local variable of the 'innerFunction' function
> named 'x' and the assignment of the value 5 to that variable.

Not possible, the creation thing again, see 1.


> 10. The assignment of the value 5 to a declared local variable of the
> 'innerFunction' function named 'x'.

Possible, innerFunction's execution object is on the scope chain.


> 11. The creation of a global variable named 'x' and the assignment of
> the value 5 to that variable.

Possible if x is not encountered sooner in the scope chain, see 1.
Unless you are splitting hairs by saying that properties created this
way aren't variables because they aren't declared, they are just
properties.


> 12. The assignment of the value 5 to a declared global variable
> named 'x'.

Possible, see 11.


> 13. The creation of an 'x' property of the global object and the
> assignment of the value 5 to that property.

Possible, see 11. If all else fails...


> 14. The assignment of the value 5 to a pre-existing 'x' property of
> the global object.

Possible, not much different to 12: declaring x as a global variable
makes it pre-existing to the execution of any code.


> 15. The creation of an 'x' property of the window object and the
> assignment of the value 5 to that property.

Possible, where window === global (which should be always for
browsers), as for 11.


> 16. The assignment of the value 5 to a pre-existing 'x' property of
> the window object.

Possible, see 14 & 15.


> 17. A runtime error.

Possible if anObjectReference isn't an object.


> Q2: If the line of code above is changed from - x = 5; - to - var x =
> 5 - which (group of) the above are then the possible outcomes of the
> execution of that line?

x becomes a property of the innerFunction activation object, the only
possible outcomes are that the value will be assigned to an existing
property anObjectReference, or if that doesn't exist, innerFunction's
declared local x. The resolution of x will proceed no further so no
other outcome will occur.


> I would have to go over the answers with the candidate taking the test
> as there are a number of 'understandable mistakes' to be easily made
> here (that is, getting some of them wrong is a certain fail, but others
> may need the thinking behind the answer.)

Hopefully my explanations are sufficient where the answer itself
isn't.


--
Rob

RobG

unread,
Aug 1, 2007, 10:11:09 AM8/1/07
to
On Aug 1, 5:32 pm, marss <marss...@gmail.com> wrote:
> On 1 , 02:43, "Richard Cornford" <Rich...@litotes.demon.co.uk>
> wrote:
[...]

> > /* unknown global code */
> > function outerFunction(){
> > /* unknown outer function body code */
> > function innerFunction(){
> > /* unknown inner function body code */
> > with(anObjectReference){
> > x = 5; //<--- The subject line of code.
> > }
> > /* more unknown inner function body code */
> > }
> > /* more unknown outer function body code */}
>
> > /* more unknown global code */
>
> If I were HR manager I most likely do not hire a person who writes
> code in that manner. Not because I am afraid of people who know more
> than me. Gurus come and go but code leaves. Practice shows that
> unmaintable code worse than inefficient code.

That pattern is common where the intention is to emulate private
methods or to encapsulate a library of functions, though it is
normally written something like:

var outerFunction = (function()
{
function innerFunction(){...}

/*
** code that calls innerFunction
*/

})();

The intention of Richard's question is not to encourage any particular
code style or use of the with statement (or even to show off), but to
propose a set of questions to determine someone's understanding of
basic identifier resolution against the scope chain. The use of a
reasonably common code pattern tests if they can apply that knowledge.

It may also test if they can read the ECMAScript Language spec and
apply it, supposing they are as unfamiliar with the with statement as
the great majority of those here seem to be (and I count myself as one
of them).

If you are an HR manager and not a programmer, get someone who you
trust to hire programmers. :)

What's more dangerous, a manager who codes or a programmer with a
soldering iron?

--
Rob.

Peter Michaux

unread,
Aug 1, 2007, 12:56:39 PM8/1/07
to
Hi Rob,

Not that I think I'm a qualified know-it-all but in the spirit of
discussion...

On Aug 1, 6:50 am, RobG <rg...@iinet.net.au> wrote:

> > 2. The assignment of the value 5 to a pre-existing 'x' property of
> > the 'outerFunction' function.
>
> Possible, if anObjectReference does not have an x property.

I think the only way to set a prexisting x *property* of outerFunction
would be if anObjectReference refers to outerFunction. In this case
anObjectReference would have an x property.


> > 4. The assignment of the value 5 to a pre-existing 'x' property of
> > the 'innerFunction' function.
>
> Not possible, the innerFunction function object might have an x
> property, but it isn't on the scope chain. However, innerFunction's
> execution object (which is on the scope chain) may have an x property
> if one's been declared and that could be assigned a value.

If anObjectReference refers to the innerFunction object then the
properties of innerFunction are in the scope chain.


> > 17. A runtime error.
>
> Possible if anObjectReference isn't an object.

But this runtime error isn't due to the subject line of code. It is
due to the line before it.

Peter

Peter Michaux

unread,
Aug 1, 2007, 1:22:20 PM8/1/07
to
On Jul 31, 10:42 pm, David Mark <dmark.cins...@gmail.com> wrote:
> On Aug 1, 12:46 am, Peter Michaux <petermich...@gmail.com> wrote:
>
> > On Jul 31, 6:35 pm, David Mark <dmark.cins...@gmail.com> wrote:
>
> > > On Jul 31, 7:43 pm, "Richard Cornford" >
> > > > 17. A runtime error.
>
> > > var anObjectReference;
>
> > > function outerFunction(){
> > > function innerFunction() {
> > > with(anObjectReference){
> > > x = 5; //<--- The subject line of code.
> > > }
> > > }
> > > innerFunction()
> > > }
> > > outerFunction();
>
> > I believe the error here isn't coming from the subject line of code
> > but the line before it.
>
> Correct. That's one I missed due to not following instructions to the
> letter.
>
>
>
> > A runtime error could still occur due to the subject line of
> > JavaScript. Taking it to the absurd, I have no clue what the
> > programmer who embedded JavaScript in a particular application has
> > done with the global x property. It may be that setting the global x
> > property always results in a runtime error.
>
> I don't quite follow you there.

I'm thinking of a non-browser embedding of JavaScript where the
programmer to whom I refer and who has made the embedding has created
a x property of the global object. This programmer can create a
genuine JavaScript runtime error for virtually anything. This isn't a
runtime error due to any of the code in the JavaScript engine (eg
spidermonkey) but it is a JavaScript runtime error nonetheless. Like I
said, this is a relatively absurd answer.


> I find no real joy in writing JavaScript or any other sort of code. I
> do enjoy designing and building applications (somewhat), but the
> actual coding is drudgery to me.

I can understand this sentiment at times :) However using higher order
functions and to dramatically reduce the amount of code I need to
write and maintain can be a real joy just for the ingenuity of it all.

Peter

Peter Michaux

unread,
Aug 1, 2007, 1:29:02 PM8/1/07
to
On Jul 31, 4:43 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
>
> "Inspired" by one of the more ambiguous questions on your meebo.com page
> I thought the following might make quite interesting written test
> questions, and give an impression of my thought process in setting
> javascript questions:-
>
> /* unknown global code */
> function outerFunction(){
> /* unknown outer function body code */
> function innerFunction(){
> /* unknown inner function body code */
> with(anObjectReference){
> x = 5; //<--- The subject line of code.
> }
> /* more unknown inner function body code */
> }
> /* more unknown outer function body code */}
>
> /* more unknown global code */
>
> /* ********************************************************\
> | Note: Three facts about the 'unknown' code:- |
> | |
> | 1. There are no more function definitions, no function |
> | expressions and no uses of the Function constructor. |
> | 2. There are no - with - statements in the unknown code.|
> | 3. There are no uses of the - eval - function. |
> \******************************************************** */

I started thinking about strings in JavaScript that can be used like
eval() or the function constructor. I think that no use of
setTimeout() or setInterval() with a string first argument should be
added to the list of facts also.

dhtmlk...@gmail.com

unread,
Aug 1, 2007, 4:20:38 PM8/1/07
to
On Jul 31, 5:21 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
Yep. It's pretty straightforward, although if you know of any
implementation bugs, please let us know.

Hint: What are the built-in objects? (process of elimination
strategy).

> > 2. Define p and q (they are not Strings). Result:
> > p < q; // false.
> > p <= q; // true.
> > p == q; // false.
>
> What about the execution order of these tests, as a strict adherence to
> the order shown greatly expands the possibilities?
>
> Richard.

Execution order is irrelevant, so you could have also:
p == q; // false
p <= q; // true
p < q; // false

However, if you do know a way in which execution order would make a
difference, please explain.
Hint: This is a test of how operators work.

Thomas 'PointedEars' Lahn

unread,
Aug 1, 2007, 4:41:02 PM8/1/07
to

Not possible. For the creation of a property of a Function object that
object needs to be referenced explicitly.

> 2. The assignment of the value 5 to a pre-existing 'x' property of
> the 'outerFunction' function.

Not possible. For the assignment to a property of a Function object
that object needs to be referenced explicitly.

> 3. The creation of an 'x' property of the 'innerFunction' function
> and the assignment of the value 5 to that property.

Same here.

> 4. The assignment of the value 5 to a pre-existing 'x' property of
> the 'innerFunction' function.

And here.

> 5. The creation of an 'x' property of the object referred to by
> 'anObjectReference' and the assignment of the value 5 to that
> property.

At least unlikely. The ambiguity in the `with' statement is that it
usually does not apply to assignment statements without a
VariableReference left-hand side. Which is why it is deprecated.

> 6. The assignment of the value 5 to a pre-existing 'x' property of
> the object referred to by 'anObjectReference'.

Same here.

> 7. The creation of a local variable of the 'outerFunction' function
> named 'x' and the assignment of the value 5 to that variable.

Not possible. For the creation of local variables requires a local
VariableStatement, however a VariableStatement would make the variable
one of innerFunction().

> 8. The assignment of the value 5 to a declared local variable of the
> 'outerFunction' function named 'x'.

Possible. The innerFunction() function must not have a local `x'
variable then.

> 9. The creation of a local variable of the 'innerFunction' function
> named 'x' and the assignment of the value 5 to that variable.

Not possible for the reasons given for 7.

> 10. The assignment of the value 5 to a declared local variable of the
> 'innerFunction' function named 'x'.

Possible.

> 11. The creation of a global variable named 'x' and the assignment of
> the value 5 to that variable.

Possible if neither innerFunction() nor outerFunction() have a variable
with that name declared.

> 12. The assignment of the value 5 to a declared global variable
> named 'x'.

Same here.

> 13. The creation of an 'x' property of the global object and the
> assignment of the value 5 to that property.

Possible. Global variables are properties of the Global Object, because
that is the Variable Object of the global execution context.

> 14. The assignment of the value 5 to a pre-existing 'x' property of
> the global object.

Possible if neither innerFunction() nor outerFunction() have a variable
with that name declared.

> 15. The creation of an 'x' property of the window object and the
> assignment of the value 5 to that property.

Possible, although the outcome is implementation- and context-dependent.
The host-defined `window' property of the Global Object may not refer
to the Global Object, of which the `x' property would be a property of
under the conditions mentioned for 14.

> 16. The assignment of the value 5 to a pre-existing 'x' property of
> the window object.

Same here.

> 17. A runtime error.

Possible if x was not declared before and x is an ID or a name of a DOM
object of the IE DOM (MSHTML component). I can also think of broken
implementations that would not allow a variable to have the same ID or
name as a DOM object.

> Q2: If the line of code above is changed from - x = 5; - to - var x =
> 5 - which (group of) the above are then the possible outcomes of the
> execution of that line?

9 and 10.

> I would have to go over the answers with the candidate taking the test
> as there are a number of 'understandable mistakes' to be easily made
> here (that is, getting some of them wrong is a certain fail, but others
> may need the thinking behind the answer.)

I am looking forward to your evaluation of my answers.


Regards,
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

RobG

unread,
Aug 1, 2007, 5:04:03 PM8/1/07
to
On Aug 2, 2:56 am, Peter Michaux <petermich...@gmail.com> wrote:
> Hi Rob,
>
> Not that I think I'm a qualified know-it-all but in the spirit of
> discussion...
>
> On Aug 1, 6:50 am, RobG <rg...@iinet.net.au> wrote:
>
> > > 2. The assignment of the value 5 to a pre-existing 'x' property of
> > > the 'outerFunction' function.
>
> > Possible, if anObjectReference does not have an x property.
>
> I think the only way to set a prexisting x *property* of outerFunction
> would be if anObjectReference refers to outerFunction. In this case
> anObjectReference would have an x property.
>
> > > 4. The assignment of the value 5 to a pre-existing 'x' property of
> > > the 'innerFunction' function.
>
> > Not possible, the innerFunction function object might have an x
> > property, but it isn't on the scope chain. However, innerFunction's
> > execution object (which is on the scope chain) may have an x property
> > if one's been declared and that could be assigned a value.
>
> If anObjectReference refers to the innerFunction object then the
> properties of innerFunction are in the scope chain.

My logic was faulty on both questions - I never considered that
anObjectReference might be to inner/outerFunction. So I got 2 "right"
by accident, and 4 wrong on purpose. I think the tedium got to me!


> > > 17. A runtime error.
>
> > Possible if anObjectReference isn't an object.
>
> But this runtime error isn't due to the subject line of code. It is
> due to the line before it.

Which means that the line itself is never executed - perhaps a
question along those lines would be more appropriate. It would also
be more difficult to fathom, most people are unlikely to consider
runtime errors as a strategy for preventing particular lines of code
from executing :-).


--
Rob

Thomas 'PointedEars' Lahn

unread,
Aug 1, 2007, 5:22:39 PM8/1/07
to
RobG wrote:
> On Aug 2, 2:56 am, Peter Michaux <petermich...@gmail.com> wrote:
>> On Aug 1, 6:50 am, RobG <rg...@iinet.net.au> wrote:
>>>> 2. The assignment of the value 5 to a pre-existing 'x' property of
>>>> the 'outerFunction' function.
>>> Possible, if anObjectReference does not have an x property.
>> I think the only way to set a prexisting x *property* of outerFunction
>> would be if anObjectReference refers to outerFunction. In this case
>> anObjectReference would have an x property.
>>
>>>> 4. The assignment of the value 5 to a pre-existing 'x' property of
>>>> the 'innerFunction' function.
>>> Not possible, the innerFunction function object might have an x
>>> property, but it isn't on the scope chain. However, innerFunction's
>>> execution object (which is on the scope chain) may have an x property
>>> if one's been declared and that could be assigned a value.
>> If anObjectReference refers to the innerFunction object then the
>> properties of innerFunction are in the scope chain.
>
> My logic was faulty on both questions - I never considered that
> anObjectReference might be to inner/outerFunction. [...]

However, Peter is mistaken here. The `with' statement does not apply to
the left-hand side of this assignment, no matter the object referenced
with anObjectReference. Unless x is declared, it will always reference
a property of the Global Object, that is a global variable. And so it
will either create that, modify it, or due to the peculiarities of an
execution environment cause a runtime error.

Peter Michaux

unread,
Aug 1, 2007, 5:29:45 PM8/1/07
to
On Aug 1, 2:04 pm, RobG <rg...@iinet.net.au> wrote:
>
>
> > > > 17. A runtime error.
>
> > > Possible if anObjectReference isn't an object.
>
> > But this runtime error isn't due to the subject line of code. It is
> > due to the line before it.
>
> Which means that the line itself is never executed - perhaps a
> question along those lines would be more appropriate. It would also
> be more difficult to fathom, most people are unlikely to consider
> runtime errors as a strategy for preventing particular lines of code
> from executing :-).

Oddly enough . . . I've done this! I considered run-time errors due to
a browser bug/limitation to determine which versions of a particular
old browser could make an XHR POST. This was probably the weirdest
feature test I've ever made but avoided a use of navigator.userAgent.

// NN6.2 can't make POST requests because can't have arguments to
send()
// so now catch NN6.2 and any other browsers that can't take
argument to XHR.send()
function cannotPost() {
var xhr = new XMLHttpRequest();
try {
xhr.send("asdf");
} catch (e) {
// All calls to xhr.send() should error because there wasn't a
call to xhr.open()
// however the normal error is something about "not initialized"
as expected
// since xhr.open() was not called. NN6.2 gives a different
error indicating
// xhr.send() cannot take arguments.
if (-1 !== e.toString().indexOf("Could not convert JavaScript
argument arg 0 [nsIXMLHttpRequest.send]")) {
return true;
}
}
return false;
}


Peter

Peter Michaux

unread,
Aug 1, 2007, 5:51:11 PM8/1/07
to
On Aug 1, 2:22 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:

> RobG wrote:
> > On Aug 2, 2:56 am, Peter Michaux <petermich...@gmail.com> wrote:
> >> On Aug 1, 6:50 am, RobG <rg...@iinet.net.au> wrote:
>
> >>>> 4. The assignment of the value 5 to a pre-existing 'x' property of
> >>>> the 'innerFunction' function.

> >>> Not possible, the innerFunction function object might have an x
> >>> property, but it isn't on the scope chain. However, innerFunction's
> >>> execution object (which is on the scope chain) may have an x property
> >>> if one's been declared and that could be assigned a value.
> >> If anObjectReference refers to the innerFunction object then the
> >> properties of innerFunction are in the scope chain.
>
> > My logic was faulty on both questions - I never considered that
> > anObjectReference might be to inner/outerFunction. [...]
>
> However, Peter is mistaken here. The `with' statement does not apply to
> the left-hand side of this assignment, no matter the object referenced
> with anObjectReference. Unless x is declared,

Richard's #4 states that when the subject line executes that a
innerFunction.x does already exist.

> it will always reference
> a property of the Global Object, that is a global variable. And so it
> will either create that, modify it, or due to the peculiarities of an
> execution environment cause a runtime error.

I ran the following in Mac/Firefox2 and it indicates a "yes" to
Richard's statement.

function outerFunction() {
innerFunction.x = 2;
var anObjectReference = innerFunction;
function innerFunction() {
with (anObjectReference) {
x=5;
}
}
innerFunction();
alert(innerFunction.x); // 5
}
outerFunction();

Thomas 'PointedEars' Lahn

unread,
Aug 1, 2007, 5:59:07 PM8/1/07
to
Peter Michaux wrote:
> | 4. The assignment of the value 5 to a pre-existing 'x' property of
> | the 'innerFunction' function.
>
> [...]

> I ran the following in Mac/Firefox2 and it indicates a "yes" to
> Richard's statement.
>
> function outerFunction() {
> innerFunction.x = 2;
> var anObjectReference = innerFunction;
> function innerFunction() {
> with (anObjectReference) {
> x=5;
> }
> }
> innerFunction();
> alert(innerFunction.x); // 5
> }
> outerFunction();

Confirmed for Firefox 2 on Windows XP. Thanks for surprising me.

David Mark

unread,
Aug 1, 2007, 6:15:42 PM8/1/07
to
On Aug 1, 5:59 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:

> Peter Michaux wrote:
> > | 4. The assignment of the value 5 to a pre-existing 'x' property of
> > | the 'innerFunction' function.
>
> > [...]
> > I ran the following in Mac/Firefox2 and it indicates a "yes" to
> > Richard's statement.
>
> > function outerFunction() {
> > innerFunction.x = 2;
> > var anObjectReference = innerFunction;
> > function innerFunction() {
> > with (anObjectReference) {
> > x=5;
> > }
> > }
> > innerFunction();
> > alert(innerFunction.x); // 5
> > }
> > outerFunction();
>
> Confirmed for Firefox 2 on Windows XP. Thanks for surprising me.
>

You wouldn't be surprised at all if you had read the thread before
posting.

Richard Cornford

unread,
Aug 1, 2007, 7:04:13 PM8/1/07
to
On Aug 1, 2:35 am, David Mark wrote:
> On Jul 31, 7:43 pm, Richard Cornford wrote:
<snip>

>> Q2: If the line of code above is changed from - x = 5; - to
>> - var x = 5 - which (group of) the above are then the possible
>> outcomes of the execution of that line?
>
> And perhaps that one was a trick question.

You seem to have missed the second question entirely.

> I'm starting to wonder if
> any of the "dunno" scenarios above are even possible.

The question is the assessment of which are possible and which are not.

> Since I never use with clauses, this test would be a
> nightmare for me.

Not using the - with - statement is not a reason for not knowing what it
does (which is simply to add an object to the top of the scope chain)
and the implications of what it does (particularly for the resolution of
Identifiers against that modified scope chain).

> And yes, I verified some of my answers with alerts. All but a
> couple were correct (and I corrected those that weren't.)
>
> And aren't with clauses supposed to be taboo in JS?
>
>> I would have to go over the answers with the candidate
>> taking the test as there are a number of 'understandable
>> mistakes' to be easily made here (that is, getting some
>> of them wrong is a certain fail, but others may need the
>> thinking behind the answer.)
>
> I figure I failed,

Hard to say without all of the answers. You did manage to spot how the
value might get assigned to 'x' properties of the function objects,
which is counter intuitive and so something I would not be surprised to
see go unobserved. And the point of such a test is expose the boundaries
of someone's understanding, for which it is necessary to try to pitch a
few shots over the target.

> but this seems more of an academic exercise than a
> practical test

It is certainly in their nature for technical tests to look like
academic exercises. The practical purposes they serve are the purposes
of the people asking the questions.

> (and I am no expert on the guts of ECMAScript.) This
> is reinforced by the fact that I haven't used a single
> with clause in ten years of scripting Web pages/applications.

But do you understand why you don't use them? That is an implied part of
the question I asked. The use of - with - statement is strongly
discouraged precisely because some of the outcomes where it is used are
distinctly counterintuitive, and that makes for hard to understand, and
so difficult/expensive to maintain code.

> I've exploited
> closures once (thanks to a tip in one of your articles) and
> I never nest functions in functions. I've never found a
> practical need to do any of these things. Furthermore, using
> closures, nested functions, etc. would seem a bad idea if you
> consider the people who have to maintain the code in the future
> (most of whom will likely be the incompetent clipboard jockeys
> you alluded to in your preface.)

That is an oft-repeated position, though I don't think its proponents
have considered what the consequences are if the limits imposed upon the
creation of javascript are that the results should always be
comprehendible to the ignorant and incompetent.

Did you notice the post advertising http://www.wikicodia.com last week?
Go to the home page of that site and look at the script they have there;
evidence of an author who does not know what a local variable is,
doesn't know how, when and why to use them, and has created a script
that has wrapped needless javascript dependency round what must be
completely viable server-side search facility for the sake of a trivial
gimmick. And I should limit my code to this level just so this
individual will not be obviously out of his depth in the event that some
fool is reckless enough to employ him to maintain it?

Richard.

Richard Cornford

unread,
Aug 1, 2007, 7:04:10 PM8/1/07
to
On Aug 1, 5:15 am, Peter Michaux wrote:

> On Jul 31, 4:43 pm, Richard Cornford wrote:
> [snip]
>
>> But then I am very cynical.
>
> At least you know! :)
>
>> I have the problem of setting javascript technical tests for
>> interviews (not that often, but it is part of my job), so from

>> time to time I think about what questions they should include,
>> and what I would be looking for in an answer.
>
> Given your apparent high standards and opinion of the general
> quality of JavaScript programmers, how do these employee
> searches go for you?

It is not an easy process. If you advertise of expertise you still get a
large proportion of people applying who don't even know the basics (or,
what I would consider to be the basics).

It seems to be a common problem.

> Do you simply take the best applicant or do you wait for a
> satisfactory applicant?

The potential for extreme and long term harm that can follow form
employing the wrong person is such that nobody gets in unless they
really can do the job, or clearly show the potential to learn the job
very quickly. The "best applicant" is rarely that person.

>> For example, as a verbal question I would tend to ask;
>> "which side of an inequality expression is evaluated first?" Not
>> because I want to be told the answer (I would not memorise that
>> sort of detail (after all, it does not matter 99.9% of the time),
>> and it would scare me to encounter someone who did), but because
>> I would want to hear "I would have to look it up" (or words to that
>> effect), so I could ask where they would look it up, and if given
>> the correct answer hand them a copy of the document to see if they
>> were familiar enough with it (and interpreting it) to give me the
>> correct answer quickly.
>
> If I was feeling bold and relatively happy with my current
> job, I would answer that my code should not depend on
> knowledge of which side is evaluated first.

That would not necessarily be a bad answer, if you could come up with
something well-reasoned when asked to justify that position.

> That is information that likely anyone would have
> to look up and results in code that is not quickly read
> or easily maintained.

Consider:-

SomeObject.prototype.setState = function(newState){
if(this.state != (this.state = newState)){
// some other code
this.whenStateChanged();
}
};

Is that really difficult to understand? You don't even need to know
which side of the inequality operation is evaluated first in order to
read it, as if it is right hand side first the whole - if - expression
is doomed to always be false and there is no point in having it there at
all. On the other hand, if you were writing this construct it is very
important to get it the right way around, and as we are agreed that
nobody is going to memorise this level of detail it becomes important to
be able to find out the order of evaluation, and preferably find out
with certainty and quickly (less than 30 seconds).

And I think a good javascript programmer knows why we don't use the -
with - statement. That is, knows why the results are "hard to maintain,
prone to creating properties in the wrong place", and for that they need
to know what it does (which is no more than place an object on the top
of the scope chain).

> These warnings about "with" are plastered all over the place

But being "plastered all over the place" is hardly any indicator of
truth when it comes to javascript. After all, how often do you read the
assertion that nobody should use closures because closures cause memory
leaks on IE, which is an overly extreme reaction following form a
misrepresentation of the facts. If people read things "plastered all
over the place" they really should be looking to the explanations of why
and how (with critical judgment), and so know what - with - does without
any need actually use it.

> and a good JavaScript programmer my have never bothered to use
> "with" before.

"May have never bothered" or 'may have never chosen to use (or always
chosen not to use)'? A "good javascript programmer" will be making
informed decisions about what they use, why they use it, and what they
don't use and why they don't use it.

> The places where I have encountered "with" have been in code
> that otherwise had horrendously bad JavaScript and was written
> by programmers for whom I've come to have low respect for many
> reasons.

But does that mean you have an excuse for not knowing what - with -
does? What if you had to debug/fix/correct code of that type (code that
is likely be in need of ongoing work by virtue of the quality of its
authoring), you would have to understand it, even if you would never
write it.

> If you ever feel like posting other interesting exercises please do!

Of course I will post what I feel like, whenever I feel like it.

Richard.


Richard Cornford

unread,
Aug 1, 2007, 7:04:17 PM8/1/07
to
On Aug 1, 2:48 am, David Mark wrote:
> On Jul 31, 8:21 pm, Richard Cornford wrote:
<snip>

>>> 2. Define p and q (they are not Strings). Result:
>>> p < q; // false.
>>> p <= q; // true.
>>> p == q; // false.
>
>> What about the execution order of these tests, as a strict adherence
>> to the order shown greatly expands the possibilities?
>
> I don't understand what you mean by that.

In the event that either one value is the primitive null value and the
other is zero or false (the empty string being precluded by the stated
condition), then the tests will produce the results shown. In addition,
if p and q are distinct objects and the - valueOf - or (if they don't
use the default object - valueOf - methods, or similar) - toString -
methods are defined such that one returns a null value and the other
returns one of zero, false, or the empty string (which is not precluded
by the conditions as the p and q values are objects) then you also get
shown results.

So in addition to the 4 possible permutations of primitive values that
are correct answers to the question there are an infinite number of
possible answers using objects, a simple example of which might be:-

p = {valueOf:function(){return null;}};
q = {valueOf:function(){return '';}};

However, if the tests are applied in a specific order then you can
include Object where the - valueOf - or - toString - methods return
values that differ sequentially to satisfy the test being made. Adding
another infinite number of possibly correct answers to the list. Hence
"greatly expands the possibilities".

> I assume the question meant to define the values once and
> then evaluate them in whatever order.

There is great danger in reading more into statements than they state.
It is the sort of thinking that has had people making multi-million
dollar space probes and then using them to discover that the surface of
Mars is not really that bouncy. If a specification is not sufficiently
specific it is invariably best to ask for clarification.

> Do you mean that you could re-assign p and q in between each
> test and come up with virtually infinite possibilities?

No, I don't see how p or q could be re-defined/re-assigned give the
conditions in the question ("Define p and q" being singular), but I have
could come up with infinite possibilities anyway.

Richard.

Richard Cornford

unread,
Aug 1, 2007, 7:04:07 PM8/1/07
to
On Aug 1, 6:42 am, David Mark wrote:
> On Aug 1, 12:46 am, Peter Michaux wrote:
>> On Jul 31, 6:35 pm, David Mark wrote:

>>> On Jul 31, 7:43 pm, Richard Cornford wrote:
>>>> 17. A runtime error.
>
>>> var anObjectReference;
>
>>> function outerFunction(){
>>> function innerFunction() {
>>> with(anObjectReference){
>>> x = 5; //<--- The subject line of code.
>>> }
>>> }
>>> innerFunction()
>>> }
>>> outerFunction();
>
>> I believe the error here isn't coming from the subject line
>> of code but the line before it.
>
> Correct. That's one I missed due to not following instructions
> to the letter.

Except the letter reads "Assuming the line that reads - x = 5; - is
executed" and an error in the previous line cannot have happened if the
line is executed.

>> A runtime error could still occur due to the subject line of
>> JavaScript. Taking it to the absurd, I have no clue what the
>> programmer who embedded JavaScript in a particular application
>> has done with the global x property. It may be that setting
>> the global x property always results in a runtime error.
>
> I don't quite follow you there.

The assignment can produces a runtime error when the object being
assigned to is a host object and it throws an exception when the
assignment is made to an 'x' proeperty. Because nothing precludes the
possibility that - anObjectReference - is a host object nothing
precludes the possibility that the assignment will error.

In addition, on IE if no global 'x' variable is declared but an element
exists in the DOM with the ID attribute "x" then IE creates an 'x'
proeprty of the global object and assigns it a reference to the DOM
element. Subsequent attempts to assign to the 'x' property of the global
object then throw exceptions. So if no objects on the scope chain have
'x' properties the exception is thrown when - x = 5 - is resolved as an
assignment to the 'x' property of the global object, and in the event
that the global object were assigned to - anObjectReference - the same
exception whould be thrown with both of - x = 5; - and - var x = 5; -.

>>> I figure I failed, but this seems more of an academic exercise
>>> than a practical test (and I am no expert on the guts of
>>> ECMAScript.) This is reinforced by the fact that I haven't
>>> used a single with clause in ten years of scripting Web
>>> pages/applications. I've exploited closures once
>
>> Holy cow! What about setting scope for an event, XHR or timeout
>> callback with objects? Never anything like this...
>
> Yes, I have done that last one recently (technically with
> setInterval.) The other thing I use closures for is to associate
> DOM events with objects, which is the tip I got from Richard's
> article on the subject. But it should be mentioned that that
> particular technique will cause a memory leak in IE unless you
> clean up after it when the page unloads.
<snip>

No, the code in that article does not produce a memory leak on IE. IE's
memory leak problem has nothing to do with closures as such, it is to do
with circular chains of reference that include JS objects and COM
object. The assigning of the returned inner function form that example
to an intrinsic event property of a DOM node does result in the DOM node
having an indirect reference to a JS object, but so long as the JS
object does not then hold a reference to the DOM node the circle is not
closed and no garbage collection issues follow. The reason that the code
passes a reference to - this - on to the method of the JS object called
is so that the JS object code can have a reference to the DOM node when
it needs one, but does not have to keep one and so provoke the memory
leak issue.

On of the problems with mantra "closures cause memory leaks on IE" is
that it brushes over the fact that circular references can be
established without closures, and doing so is trivial and inherent in
many commonly promoted scripting practices. Another case where knowing
what you are doing is infinitely better than listening to some vague
general advice on scripting.

Richard.

David Mark

unread,
Aug 1, 2007, 7:18:45 PM8/1/07
to
On Aug 1, 7:04 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:

> On Aug 1, 6:42 am, David Mark wrote:
>
>
>
>
>
> > On Aug 1, 12:46 am, Peter Michaux wrote:
> >> On Jul 31, 6:35 pm, David Mark wrote:
> >>> On Jul 31, 7:43 pm, Richard Cornford wrote:
> >>>> 17. A runtime error.
>
> >>> var anObjectReference;
>
> >>> function outerFunction(){
> >>> function innerFunction() {
> >>> with(anObjectReference){
> >>> x = 5; //<--- The subject line of code.
> >>> }
> >>> }
> >>> innerFunction()
> >>> }
> >>> outerFunction();
>
> >> I believe the error here isn't coming from the subject line
> >> of code but the line before it.
>
> > Correct. That's one I missed due to not following instructions
> > to the letter.
>
> Except the letter reads "Assuming the line that reads - x = 5; - is
> executed" and an error in the previous line cannot have happened if the
> line is executed.

Isn't that what I said? My example got it wrong as it caused an error
in the wrong line.

>
> >> A runtime error could still occur due to the subject line of
> >> JavaScript. Taking it to the absurd, I have no clue what the
> >> programmer who embedded JavaScript in a particular application
> >> has done with the global x property. It may be that setting
> >> the global x property always results in a runtime error.
>
> > I don't quite follow you there.
>
> The assignment can produces a runtime error when the object being
> assigned to is a host object and it throws an exception when the
> assignment is made to an 'x' proeperty. Because nothing precludes the
> possibility that - anObjectReference - is a host object nothing
> precludes the possibility that the assignment will error.

Okay.

>
> In addition, on IE if no global 'x' variable is declared but an element
> exists in the DOM with the ID attribute "x" then IE creates an 'x'
> proeprty of the global object and assigns it a reference to the DOM

That's odd.

> element. Subsequent attempts to assign to the 'x' property of the global
> object then throw exceptions. So if no objects on the scope chain have
> 'x' properties the exception is thrown when - x = 5 - is resolved as an
> assignment to the 'x' property of the global object, and in the event
> that the global object were assigned to - anObjectReference - the same
> exception whould be thrown with both of - x = 5; - and - var x = 5; -.

That's stranger still. Suffice to say that you wouldn't use the code
in this test in a production environment as it would be a nightmare to
maintain.

>
> >>> I figure I failed, but this seems more of an academic exercise
> >>> than a practical test (and I am no expert on the guts of
> >>> ECMAScript.) This is reinforced by the fact that I haven't
> >>> used a single with clause in ten years of scripting Web
> >>> pages/applications. I've exploited closures once
>
> >> Holy cow! What about setting scope for an event, XHR or timeout
> >> callback with objects? Never anything like this...
>
> > Yes, I have done that last one recently (technically with
> > setInterval.) The other thing I use closures for is to associate
> > DOM events with objects, which is the tip I got from Richard's
> > article on the subject. But it should be mentioned that that
> > particular technique will cause a memory leak in IE unless you
> > clean up after it when the page unloads.
>
> <snip>
>
> No, the code in that article does not produce a memory leak on IE. IE's
> memory leak problem has nothing to do with closures as such, it is to do
> with circular chains of reference that include JS objects and COM
> object. The assigning of the returned inner function form that example

Right. I was being over-cautious in cleaning up all such events on
unload. In practice they often do create circular references (eg the
object has an "element" property that references the element with the
attached event), but not always.

> to an intrinsic event property of a DOM node does result in the DOM node
> having an indirect reference to a JS object, but so long as the JS
> object does not then hold a reference to the DOM node the circle is not
> closed and no garbage collection issues follow. The reason that the code

Right.

> passes a reference to - this - on to the method of the JS object called

Interesting that you mention that. I recall that I had some
difficulty getting that part to work as expected (though perhaps I
wasn't expecting the right thing.)

> is so that the JS object code can have a reference to the DOM node when
> it needs one, but does not have to keep one and so provoke the memory
> leak issue.

That makes sense.

>
> On of the problems with mantra "closures cause memory leaks on IE" is

That is no mantra of mine.

> that it brushes over the fact that circular references can be
> established without closures, and doing so is trivial and inherent in

Right. It just so happens that the cited example of attaching DOM
events to object methods uses closures and is always one line of code
away from establishing a memory leak in IE.

David Mark

unread,
Aug 1, 2007, 7:28:30 PM8/1/07
to
On Aug 1, 7:18 pm, David Mark <dmark.cins...@gmail.com> wrote:
>
> > Except the letter reads "Assuming the line that reads - x = 5; - is
> > executed" and an error in the previous line cannot have happened if the
> > line is executed.
>
> Isn't that what I said? My example got it wrong as it caused an error
> in the wrong line.
>

Correction. Since all of my answers were technically not the
requested "possible/impossible" answers, but code examples, I got them
all wrong (at least for Q1.) Since it turns out that the error is
possible after all (though in a way I would not have foreseen), then
my type-converted answer (possible) is correct.

Applying the type-conversion, I see the answer key as:

Q1
1. Not possible
2. Possible
3. Not possible
4. Possible
5. Not possible
6. Possible
7. Not possible
8. Possible
9. Not possible
10. Possible
11. Possible
12. Possible
13. Possible
14. Possible
15. Possible
17. Possible

Q2 (multiple choice)
9.

Most of the other posters had similar takes. What are the correct
answers as you see them?

zero...@gmail.com

unread,
Aug 1, 2007, 7:35:28 PM8/1/07
to
1. The creation of an 'x' property of the 'outerFunction' function
and the assignment of the value 5 to that property.

function outerFunction() {
var anObjectReference = outerFunction;
Function.prototype.x = 0;
function innerFunction() {
with (anObjectReference) {
x = 5;
}
}
innerFunction();
}
outerFunction();


3. The creation of an 'x' property of the 'innerFunction' function
and the assignment of the value 5 to that property.

function outerFunction() {
function innerFunction() {
var anObjectReference = innerFunction;
Function.prototype.x = 0;
with (anObjectReference) {
x = 5;
}
}
innerFunction();
}
outerFunction();


5. The creation of an 'x' property of the object referred to by

'anObjectReference' and the assignment of the value 5 to that
property.

function outerFunction() {
function innerFunction() {
var anObjectReference = {};
Object.prototype.x = 0;
with (anObjectReference) {
x = 5;
}
}
innerFunction();
}
outerFunction();

Peter Michaux

unread,
Aug 1, 2007, 7:38:41 PM8/1/07
to

10 is possible also since it is possible to write "var x" more than
once in a function.

var anObjectReference = {};

function outerFunction(){

function innerFunction(){
var x = 2;
with(anObjectReference){
var x = 5; //<--- The subject line of code.
}
alert(x); // 5
}
innerFunction();
}
outerFunction();

Richard Cornford

unread,
Aug 1, 2007, 7:36:44 PM8/1/07
to
"Thomas 'PointedEars' Lahn" wrote:
<snip>

> I am looking forward to your evaluation of my answers.

I hope you and everyone else who has had a go doesn't mind if I leave
going into my version of the answers for a couple of days (probably the
weekend). That should give everyone who wants to a chance to have a go,
and revise their answers if they have reason to re-evaluate them.

Richard.

Richard Cornford

unread,
Aug 1, 2007, 7:36:49 PM8/1/07
to
Thomas 'PointedEars' Lahn wrote:
> Peter Michaux wrote:
>> | 4. The assignment of the value 5 to a pre-existing 'x'
>> | property of the 'innerFunction' function.
>>
>> [...]
>> I ran the following in Mac/Firefox2 and it indicates a
>> "yes" to Richard's statement.
>>
>> function outerFunction() {
>> innerFunction.x = 2;
>> var anObjectReference = innerFunction;
>> function innerFunction() {
>> with (anObjectReference) {
>> x=5;
>> }
>> }
>> innerFunction();
>> alert(innerFunction.x); // 5
>> }
>> outerFunction();
>
> Confirmed for Firefox 2 on Windows XP. Thanks for
> surprising me.

I am surprised that you are surprised by that one (David showed that
this morning). The one that is likely to get the attention is the proof
that #1, #3 and #5 are possible (clue: the wording is deliberately
precise)

Richard.

Richard Cornford

unread,
Aug 1, 2007, 7:36:57 PM8/1/07
to
Peter Michaux wrote:
<snip>

> // NN6.2 can't make POST requests because can't have
> arguments to send()
> // so now catch NN6.2 and any other browsers that can't
> take
> argument to XHR.send()
> function cannotPost() {
> var xhr = new XMLHttpRequest();
> try {
> xhr.send("asdf");
> } catch (e) {
> // All calls to xhr.send() should error because there
> wasn't a call to xhr.open()
> // however the normal error is something about "not
> initialized" as expected
> // since xhr.open() was not called. NN6.2 gives a different
> error indicating
> // xhr.send() cannot take arguments.
> if (-1 !== e.toString().indexOf("Could not convert JavaScript
> argument arg 0 [nsIXMLHttpRequest.send]")) {
<snip>

Did you try (if they actually exist) versions in other languages? One of
the problems with try-catch in javascript is that the specification
leaves a great deal of leeway in the definition of the Error objects.
They already vary considerably in the messages they use between browsers
and if they also vary in language that is even worse.

Richard.

Peter Michaux

unread,
Aug 1, 2007, 7:41:42 PM8/1/07
to
On Aug 1, 4:35 pm, zerog...@gmail.com wrote:
> 1. The creation of an 'x' property of the 'outerFunction' function
> and the assignment of the value 5 to that property.
>
> function outerFunction() {
> var anObjectReference = outerFunction;
> Function.prototype.x = 0;
> function innerFunction() {
> with (anObjectReference) {
> x = 5;
> }
> }
> innerFunction();}
>
> outerFunction();

Wow!

Nice one.

Peter

David Mark

unread,
Aug 1, 2007, 7:50:26 PM8/1/07
to
On Aug 1, 7:38 pm, Peter Michaux <petermich...@gmail.com> wrote:
> > Q2 (multiple choice)
> > 9.
>
> 10 is possible also since it is possible to write "var x" more than
> once in a function.
>
> var anObjectReference = {};
>
> function outerFunction(){
>
> function innerFunction(){
> var x = 2;
> with(anObjectReference){
> var x = 5; //<--- The subject line of code.
> }
> alert(x); // 5
> }
> innerFunction();}
>
> outerFunction();
>

Right. I didn't think of that one.

Peter Michaux

unread,
Aug 1, 2007, 7:55:08 PM8/1/07
to
On Aug 1, 4:04 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
>

> The potential for extreme and long term harm that can follow form
> employing the wrong person is such that nobody gets in unless they
> really can do the job, or clearly show the potential to learn the job
> very quickly. The "best applicant" is rarely that person.

Just out of curiosity to which types of "extreme and long term harm"
can result from JavaScript at your company? When boiled down, most
JavaScript jobs involve widgets and XHR requests. I get the impression
that you get to do something more risky/interesting.

Peter

Peter Michaux

unread,
Aug 1, 2007, 8:01:35 PM8/1/07
to
On Aug 1, 4:36 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:

In other languages? You mean JScript vs JavaScript?

The idea here is that I want to discard all browsers that throw this
particular error string. Since I am not testing a real XHR request,
and all browsers throw an error here, there is a slight chance I am
counting some browsers that can post as a browser that cannot post.
That is if they throw the same error as the Netscape browser in
question. I tested a lot of browsers (including all point versions I
could get around the sticky version of Netscape) and this test seemed
to be just right.

Peter

David Mark

unread,
Aug 1, 2007, 8:04:27 PM8/1/07
to

Don't 1 and 3 violate the instruction not to use the Function
constructor. I am not sure if what you did constitutes "use" or not.

5 seems wrong to me in that the property x already exists.

Richard Cornford

unread,
Aug 1, 2007, 8:06:03 PM8/1/07
to
David Mark wrote:
> On Aug 1, 7:18 pm, David Mark wrote:
<snip>
> ... . What are the correct

> answers as you see them?

Later (probably the weekend). I am sure that everyone will want
explanations of why I think my answers stand, not least because everyone
will want to find at least one fault with them. ;-)

Richard.

Richard Cornford

unread,
Aug 1, 2007, 8:06:04 PM8/1/07
to
David Mark wrote:
> On Aug 1, 7:04 pm, Richard Cornford wrote:
<snip>

>> In addition, on IE if no global 'x' variable is declared but
>> an element exists in the DOM with the ID attribute "x" then
>> IE creates an 'x' proeprty of the global object and assigns
>> it a reference to the DOM
>
> That's odd.

It is not that odd, they have always done that. It is widely considered
a bad idea and it certainly directly leads to many cross-browser issues.

>> element. Subsequent attempts to assign to the 'x' property
>> of the global object then throw exceptions. So if no objects
>> on the scope chain have 'x' properties the exception is thrown
>> when - x = 5 - is resolved as an assignment to the 'x' property
>> of the global object, and in the event that the global object
>> were assigned to - anObjectReference - the same exception whould
>> be thrown with both of - x = 5; - and - var x = 5; -.
>
> That's stranger still. Suffice to say that you wouldn't use
> the code in this test in a production environment as it would
> be a nightmare to maintain.

<snip>

There is a piece of scripting 'best practice' advice that goes; "if you
are going to use (what effectively are) global variables you should
always explicitly declare them in the global execution context". This is
mainly a maintenance/debugging thing, because if you find something
like - x = 5; - in function body code you might wonder whether it is
intended to be global or whether a local variable declaration has been
omitted. Finding a global declaration answers the question of intent,
and not finding one suggests an error of omission, if the 'best
practice' has been strictly adhered to.

However, on IE if you declare a global variable and the DOM contains an
element with an ID that corresponds with the variable name the browser
does not assign a reference to the DOM Element to that variable, and it
does not render the variable read only. So by following this 'best
practice', and explicitly declaring all global variables used, this IE
issue is entirely avoided in production code (as a pleasant
side-effect).

Richard.

David Mark

unread,
Aug 1, 2007, 8:10:46 PM8/1/07
to
On Aug 1, 7:36 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
> that #1, #3 and #5 are possible (clue: the wording is =

Doh! That is surprising.

David Mark

unread,
Aug 1, 2007, 8:13:40 PM8/1/07
to
On Aug 1, 8:06 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:

[snip]

> practice', and explicitly declaring all global variables used, this IE
> issue is entirely avoided in production code (as a pleasant
> side-effect).

That's why I never ran into it (I always declared global variables.)
Lately I have stopped using them entirely in favor of an encapsulated
approach, which makes life even easier.

Peter Michaux

unread,
Aug 1, 2007, 8:18:06 PM8/1/07
to

This is an interesting point.

ECMA-262 15.3.2

"When |Function| is called as part of a |new| expression, it is a
constructor."

So without the |new| is isn't considered a constructor. This is like
the English words "hand" and "fist". A fist is always a hand but not
vice versa.

I didn't dig through the spec for this one but this can be done with
the Object prototype too (at least in Mac/Firefox2.)

function outerFunction() {
function innerFunction() {
var anObjectReference = innerFunction;

Object.prototype.x = 0;
with (anObjectReference) {

x = 7;
}
alert(innerFunction.x);
}
innerFunction();
}

outerFunction();

> 5 seems wrong to me in that the property x already exists.

But the existing |x| is a property of the anObjectReference's
prototype object, not of anObjectReference object itself.

What a great solution. I wish I'd thought of it.

Peter

Richard Cornford

unread,
Aug 1, 2007, 8:20:08 PM8/1/07
to

Close, but no. The condition "no uses of the Function constructor" was
applied and that is a 'use' of the Function constructor (maybe not the
expected use but a use all the same). But it is the right idea, change -
Function.prototype.x = 0; - to - Object.prototype.x = 0; - and the
functions still inherit an 'x' property through their prototype chains
(as Object.prototype is the [[Prototype]] or Function.prototype) and so
the assignment can still create an 'x' property on the function object.

Prototype chains have a well-specified role in Identifier resolution
against the scope chain so they had to play a part in the question.

Richard.

zero...@gmail.com

unread,
Aug 1, 2007, 8:25:45 PM8/1/07
to

I don't think so... or replace Function.prototype with
Object.prototype.


David Mark

unread,
Aug 1, 2007, 8:35:26 PM8/1/07
to
On Aug 1, 7:04 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:

> On Aug 1, 2:35 am, David Mark wrote:
>
> > On Jul 31, 7:43 pm, Richard Cornford wrote:
> <snip>
> >> Q2: If the line of code above is changed from - x = 5; - to
> >> - var x = 5 - which (group of) the above are then the possible
> >> outcomes of the execution of that line?
>
> > And perhaps that one was a trick question.
>
> You seem to have missed the second question entirely.

No. I got to it and it should have tipped me off to my
misinterpretation of the first one, but it didn't (at least not until
I posted my "answers" in the wrong format.)

>
> > I'm starting to wonder if
> > any of the "dunno" scenarios above are even possible.
>
> The question is the assessment of which are possible and which are not.

Yep. I missed that entirely on the first try.

>
> > Since I never use with clauses, this test would be a
> > nightmare for me.
>
> Not using the - with - statement is not a reason for not knowing what it
> does (which is simply to add an object to the top of the scope chain)

Right. I know what they do and I had a vague recollection of why they
shouldn't be used.

> and the implications of what it does (particularly for the resolution of
> Identifiers against that modified scope chain).
>
> > And yes, I verified some of my answers with alerts. All but a
> > couple were correct (and I corrected those that weren't.)
>
> > And aren't with clauses supposed to be taboo in JS?
>
> >> I would have to go over the answers with the candidate
> >> taking the test as there are a number of 'understandable
> >> mistakes' to be easily made here (that is, getting some
> >> of them wrong is a certain fail, but others may need the
> >> thinking behind the answer.)
>
> > I figure I failed,
>
> Hard to say without all of the answers. You did manage to spot how the

Yes. Though it now appears that everybody missed 1, 3 and 5. I'll
just wait for the proofs on those as I am weary of this exercise.

> value might get assigned to 'x' properties of the function objects,
> which is counter intuitive and so something I would not be surprised to
> see go unobserved. And the point of such a test is expose the boundaries
> of someone's understanding, for which it is necessary to try to pitch a
> few shots over the target.

It appears that at least a few went overhead.

>
> > but this seems more of an academic exercise than a
> > practical test
>
> It is certainly in their nature for technical tests to look like
> academic exercises. The practical purposes they serve are the purposes
> of the people asking the questions.
>
> > (and I am no expert on the guts of ECMAScript.) This
> > is reinforced by the fact that I haven't used a single
> > with clause in ten years of scripting Web pages/applications.
>
> But do you understand why you don't use them? That is an implied part of
> the question I asked. The use of - with - statement is strongly
> discouraged precisely because some of the outcomes where it is used are
> distinctly counterintuitive, and that makes for hard to understand, and
> so difficult/expensive to maintain code.

Right.

>
> > I've exploited
> > closures once (thanks to a tip in one of your articles) and
> > I never nest functions in functions. I've never found a
> > practical need to do any of these things. Furthermore, using
> > closures, nested functions, etc. would seem a bad idea if you
> > consider the people who have to maintain the code in the future
> > (most of whom will likely be the incompetent clipboard jockeys
> > you alluded to in your preface.)
>
> That is an oft-repeated position, though I don't think its proponents
> have considered what the consequences are if the limits imposed upon the
> creation of javascript are that the results should always be
> comprehendible to the ignorant and incompetent.

You have to assume that any unknown JS developer is both ignorant and
incompetent.

>
> Did you notice the post advertisinghttp://www.wikicodia.comlast week?

Unfortunately.

> Go to the home page of that site and look at the script they have there;
> evidence of an author who does not know what a local variable is,

LOL. Yep. I hate those "million monkey" sites. Written by monkeys
for monkeys.

> doesn't know how, when and why to use them, and has created a script
> that has wrapped needless javascript dependency round what must be
> completely viable server-side search facility for the sake of a trivial
> gimmick. And I should limit my code to this level just so this

Nobody should write code like that. There should be limits to
complexity, not competence.

David Mark

unread,
Aug 1, 2007, 8:42:31 PM8/1/07
to
On Aug 1, 8:18 pm, Peter Michaux <petermich...@gmail.com> wrote:
> I didn't dig through the spec for this one but this can be done with
> the Object prototype too (at least in Mac/Firefox2.)
>
> function outerFunction() {
> function innerFunction() {
> var anObjectReference = innerFunction;
> Object.prototype.x = 0;
> with (anObjectReference) {
> x = 7;
> }
> alert(innerFunction.x);
> }
> innerFunction();
>
> }
>
> outerFunction();
>

Okay, but why does this alert 0? Can the new property be created by
reference as well as assignment?

function outerFunction() {
function innerFunction() {
var anObjectReference = innerFunction;
Object.prototype.x = 0;

alert(anObjectReference.x);

Richard Cornford

unread,
Aug 1, 2007, 8:41:26 PM8/1/07
to
Peter Michaux wrote:

> On Aug 1, 4:36 pm, Richard Cornford wrote:
>> Peter Michaux wrote:
<snip>
>>> // xhr.send() cannot take arguments.
>>> if (-1 !== e.toString().indexOf("Could not convert JavaScript
>>> argument arg 0 [nsIXMLHttpRequest.send]")) {
>>
>> <snip>
>>
>> Did you try (if they actually exist) versions in other
>> languages? ..
<snip>

> In other languages? You mean JScript vs JavaScript?
<snip>

No, I meant languages like German, Spanish and Japanese. You are doing a
comparison with a phrase in English, if the phrase is available in
translation in other countries then the comparison will fail.

I seem to recall that they only did English versions of the early
Netscape 6s so you are probably OK here, but the issue exists with
try-catch and error handling in general, which is why defensive
programming to avoid errors (and so the need to attempt to handle them)
seems the better idea.

Richard.

Peter Michaux

unread,
Aug 1, 2007, 8:58:29 PM8/1/07
to
On Aug 1, 5:41 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:

Excellent points. My Anglo-centric bias shows.

I really could not find a way to use or otherwise inspect the feature
in question to determine if it would work or not. All I could manage
to do was get it to cause an error when it wouldn't work. Hence the
use of try-catch to inspect what error resulted.

Thanks,
Peter

dhtmlk...@gmail.com

unread,
Aug 2, 2007, 1:22:28 AM8/2/07
to
On Jul 31, 4:53 pm, David Mark <dmark.cins...@gmail.com> wrote:
> On Jul 31, 6:56 pm, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
> wrote:
>
>
>
> > On Jul 31, 8:29 am, Peter Michaux <petermich...@gmail.com> wrote:
>
> > > On Jul 31, 5:24 am, marss <marss...@gmail.com> wrote:
>
> > > > Maybe anyone know good free online JavaScript knowledge test? This not
> > > > exactly a system for testing online required - it may be simply list
> > > > of questions with variants of answers (I have to prepare tests for
> > > > learners and I need something to be taken as basis).
>
> > > > I was able to find only this (http://www.w3schools.com/js/
> > > > js_quiz.asp), but I need more.
>
> > >http://blog.meebo.com/?page_id=254
>
> > > Peter
>
> > Jeopardy style:
>
> > 1. b is a built-in object; not a String or string literal. Define b.
> > if( b ) {
> > alert( "if: " + typeof b );
> > }
> > else {
> > alert( "else: " + b );
> > }
> > result: alerts "if: false"
>
> I'll have to think about this one for a moment.

>
>
>
> > 2. Define p and q (they are not Strings). Result:
> > p < q; // false.
> > p <= q; // true.
> > p == q; // false.
>
> p = 0;
> q = null;

I didn't even think of that one.

dhtmlk...@gmail.com

unread,
Aug 2, 2007, 2:26:20 AM8/2/07
to
On Jul 31, 4:43 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
> Peter Michaux wrote:

> > On Jul 31, 5:24 am, marss wrote:
> >> Maybe anyone know good free online JavaScript knowledge
> >> test? This not exactly a system for testing online required
> >> - it may be simply list of questions with variants of
> >> answers (I have to prepare tests for learners and I need
> >> something to be taken as basis).
>
> >> I was able to find only this (http://www.w3schools.com/js/
> >> js_quiz.asp), but I need more.
>
> >http://blog.meebo.com/?page_id=254
>
> When the question of seeking javascript tests on the interment comes up
> my imagination usually conjures up an individual who thinks they will be
> able to get away with using other people's code and copy-pasting their
> way though life, if only they could get a foot through the door.
> Preferably into a job where they were the only person creating the
> javascript, and so not have anyone looking over their shoulders that
> knew what they were doing. The good thing about such a position is that
> the people doing the interview would not know what questions they should
> be asking and so would likely get any technical test they used off the
> Internet themselves. And so a thorough search for such tests, and the
> rote learning of the 'correct' answers, might get them past the
> technical test and into such a job.
>
> But then I am very cynical.
>
> I have the problem of setting javascript technical tests for interviews
> (not that often, but it is part of my job), so form time to time I think
> about what questions they should include, and what I would be looking
> for in an answer. For example, as a verbal question I would tend to ask;
> "which side of an inequality expression is evaluated first?" Not because
> I want to be told the answer (I would not memorise that sort of detail
> (after all, it does not matter 99.9% of the time), and it would scare me
> to encounter someone who did), but because I would want to hear "I would
> have to look it up" (or words to that effect), so I could ask where they
> would look it up, and if given the correct answer hand them a copy of
> the document to see if they were familiar enough with it (and
> interpreting it) to give me the correct answer quickly.
>
> On the other hand there are things I would expect someone to know
> (without having to look it up), and one of those is Identifier
> resolution against the scope chain.
>
> "Inspired" by one of the more ambiguous questions on your meebo.com page
> I thought the following might make quite interesting written test
> questions, and give an impression of my thought process in setting
> javascript questions:-
>
> /* unknown global code */
> function outerFunction(){
> /* unknown outer function body code */
> function innerFunction(){
> /* unknown inner function body code */
> with(anObjectReference){

> x = 5; //<--- The subject line of code.
> }
> /* more unknown inner function body code */
> }
> /* more unknown outer function body code */}
>
> /* more unknown global code */
>
I really do not like the 'with' statement.

This question looks familiar.

You don't by chance work at Google, richard?

> /* ********************************************************\
> | Note: Three facts about the 'unknown' code:- |
> | |
> | 1. There are no more function definitions, no function |
> | expressions and no uses of the Function constructor. |
> | 2. There are no - with - statements in the unknown code.|
> | 3. There are no uses of the - eval - function. |
> \******************************************************** */
>
> Q1: Assuming the line that reads - x = 5; - is executed, which (group
> of) of the following are possible outcomes of its execution?


>
> 1. The creation of an 'x' property of the 'outerFunction' function
> and the assignment of the value 5 to that property.
>

> 2. The assignment of the value 5 to a pre-existing 'x' property of
> the 'outerFunction' function.
>
> 3. The creation of an 'x' property of the 'innerFunction' function


> and the assignment of the value 5 to that property.
>

> 4. The assignment of the value 5 to a pre-existing 'x' property of
> the 'innerFunction' function.


>
> 5. The creation of an 'x' property of the object referred to by

> 'anObjectReference' and the assignment of the value 5 to that
> property.
>
This will confuse people who don't use 'with'

It's another reason I don't like 'with'; it looks like this might
happen, but instead, the property won't be created on
anObjectReference, but will be set, if it is found.


> 6. The assignment of the value 5 to a pre-existing 'x' property of
> the object referred to by 'anObjectReference'.
>
Yes.

> 7. The creation of a local variable of the 'outerFunction' function
> named 'x' and the assignment of the value 5 to that variable.
>
> 8. The assignment of the value 5 to a declared local variable of the
> 'outerFunction' function named 'x'.
Yes.

>
> 9. The creation of a local variable of the 'innerFunction' function
> named 'x' and the assignment of the value 5 to that variable.
>
> 10. The assignment of the value 5 to a declared local variable of the
> 'innerFunction' function named 'x'.
>
Yes.

> 11. The creation of a global variable named 'x' and the assignment of
> the value 5 to that variable.
>
Yes.

> 12. The assignment of the value 5 to a declared global variable
> named 'x'.
>
Yes.

> 13. The creation of an 'x' property of the global object and the


> assignment of the value 5 to that property.
>

Yes.

> 14. The assignment of the value 5 to a pre-existing 'x' property of
> the global object.
>
Yes.

> 15. The creation of an 'x' property of the window object and the


> assignment of the value 5 to that property.
>

Yes.

> 16. The assignment of the value 5 to a pre-existing 'x' property of
> the window object.
>
Yes. I thought window was the global object. Am I missing something?


> 17. A runtime error.
Yes.
- If anObjectReference is not defined.
- If x is a const.

>
> Q2: If the line of code above is changed from - x = 5; - to - var x =
> 5 - which (group of) the above are then the possible outcomes of the
> execution of that line?
>

Another reason why 'with' should be avoided. It serves only to
confuse.

I think that:
- the inner function will get a new local variable.
- anObjectRef.x won't get updated

It would seem that x would be a new variable in a scope block, but
with doesn't create a scope block, it instead conflates the current
scope block.

> I would have to go over the answers with the candidate taking the test
> as there are a number of 'understandable mistakes' to be easily made
> here (that is, getting some of them wrong is a certain fail, but others
> may need the thinking behind the answer.)
>

> Richard.

I got an interview question just like this before. The interviewer was
focused on details of the with statement and scope and the cleverness
of his question. I try to be cautious and observant about a potential
coworker's character. Code is only a part of programming; a
collaborative Art.

Garrett


dhtmlk...@gmail.com

unread,
Aug 2, 2007, 2:41:00 AM8/2/07
to
On Jul 31, 4:53 pm, David Mark <dmark.cins...@gmail.com> wrote:
> On Jul 31, 6:56 pm, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
> wrote:
>
>
>
> > On Jul 31, 8:29 am, Peter Michaux <petermich...@gmail.com> wrote:

>
> > > On Jul 31, 5:24 am, marss <marss...@gmail.com> wrote:
>
> > > > Maybe anyone know good free online JavaScript knowledge test? This not
> > > > exactly a system for testing online required - it may be simply list
> > > > of questions with variants of answers (I have to prepare tests for
> > > > learners and I need something to be taken as basis).
>
> > > > I was able to find only this (http://www.w3schools.com/js/
> > > > js_quiz.asp), but I need more.
>
> > >http://blog.meebo.com/?page_id=254
>
> > > Peter
>
> > Jeopardy style:
>
> > 1. b is a built-in object; not a String or string literal. Define b.
> > if( b ) {
> > alert( "if: " + typeof b );
> > }
> > else {
> > alert( "else: " + b );
> > }
> > result: alerts "if: false"
>
> I'll have to think about this one for a moment.
>
>
>
> > 2. Define p and q (they are not Strings). Result:
> > p < q; // false.
> > p <= q; // true.
> > p == q; // false.
>
> p = 0;
> q = null;

That is interesting. Can you explain?

I was going for the valueOf();

p=valueOf:function(){ return 1; }
q=valueOf:function(){ return 1; }

or
p= new Date(0);
q= new Date(0);
The objects are not equal.

I should have asked (and the above answer can be achieved with the
following code)

How can this be true?
p <= q; //true.
p == q; //false.
p >= q; //true.


What is the output of the following:
var x =
[
{
valueOf:function(){ return 1; }
,toString:function(){ return "one";}

}
,{
valueOf:function(){ return 1; }
,toString:function(){ return "uno";}
}
,{
valueOf:function(){ return 2; }
,toString:function(){ return "manana";}
};
].sort();

print(x);


David Mark

unread,
Aug 2, 2007, 3:38:57 AM8/2/07
to
On Aug 2, 2:41 am, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>

wrote:
> On Jul 31, 4:53 pm, David Mark <dmark.cins...@gmail.com> wrote:
>
>
>
>
>
> > On Jul 31, 6:56 pm, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
> > wrote:
>
> > > On Jul 31, 8:29 am, Peter Michaux <petermich...@gmail.com> wrote:
>
> > > > On Jul 31, 5:24 am, marss <marss...@gmail.com> wrote:
>
> > > > > Maybe anyone know good free online JavaScript knowledge test? This not
> > > > > exactly a system for testing online required - it may be simply list
> > > > > of questions with variants of answers (I have to prepare tests for
> > > > > learners and I need something to be taken as basis).
>
> > > > > I was able to find only this (http://www.w3schools.com/js/
> > > > > js_quiz.asp), but I need more.
>
> > > >http://blog.meebo.com/?page_id=254
>
> > > > Peter
>
> > > Jeopardy style:
>
> > > 1. b is a built-in object; not a String or string literal. Define b.
> > > if( b ) {
> > > alert( "if: " + typeof b );
> > > }
> > > else {
> > > alert( "else: " + b );
> > > }
> > > result: alerts "if: false"
>
> > I'll have to think about this one for a moment.
>
> > > 2. Define p and q (they are not Strings). Result:
> > > p < q; // false.
> > p = 0;
> > q = null;
>
> That is interesting. Can you explain?

According to 11.8.5 (abstract relational comparison algorithm), p and
q are both converted to 0. Clearly 0 is not less than 0.

> > > p <= q; // true.

11.8.5 applies again. 0 is equal to 0.

> > > p == q; // false.

Unlike the first two, this one is covered in 11.9.3 (abstract equality
comparison algorithm), which is a different animal. It falls all the
way through to the end and returns false.

>
[snip]

>
> What is the output of the following:
> var x =
> [
> {
> valueOf:function(){ return 1; }
> ,toString:function(){ return "one";}
>
> }
> ,{
> valueOf:function(){ return 1; }
> ,toString:function(){ return "uno";}
> }
> ,{
> valueOf:function(){ return 2; }
> ,toString:function(){ return "manana";}
> };

Typo here.

> ].sort();
>
> print(x);

In a browser? Nothing. But fix the typo and it will open the print
dialog.

Thomas 'PointedEars' Lahn

unread,
Aug 2, 2007, 7:11:24 AM8/2/07
to
Peter Michaux wrote:
> On Aug 1, 5:04 pm, David Mark <dmark.cins...@gmail.com> wrote:
>> On Aug 1, 7:35 pm, zerog...@gmail.com wrote:
>>> [Function.prototype.x = 0;]

>>
>> Don't 1 and 3 violate the instruction not to use the Function
>> constructor. I am not sure if what you did constitutes "use" or not.
>
> This is an interesting point.
>
> ECMA-262 15.3.2
>
> "When |Function| is called as part of a |new| expression, it is a
> constructor."

That is not the definition of a constructor in ECMAScript, as you can
read in one of the previous chapters of ECMA-262. So yes, the
instruction was violated.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not the
best source of advice on designing systems that use javascript.
-- Richard Cornford, <f806at$ail$1$8300...@news.demon.co.uk>

Peter Michaux

unread,
Aug 2, 2007, 11:22:26 AM8/2/07
to
On Aug 2, 4:11 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:

> Peter Michaux wrote:
> > On Aug 1, 5:04 pm, David Mark <dmark.cins...@gmail.com> wrote:
> >> On Aug 1, 7:35 pm, zerog...@gmail.com wrote:
> >>> [Function.prototype.x = 0;]
>
> >> Don't 1 and 3 violate the instruction not to use the Function
> >> constructor. I am not sure if what you did constitutes "use" or not.
>
> > This is an interesting point.
>
> > ECMA-262 15.3.2
>
> > "When |Function| is called as part of a |new| expression, it is a
> > constructor."
>
> That is not the definition of a constructor in ECMAScript, as you can
> read in one of the previous chapters of ECMA-262. So yes, the
> instruction was violated.

True the definition of "constructor" in 4.3.4 indicates the
instruction was violated; however, this definition is not consistent
with the usage of the term "constructor" in the document. There are
several places where calling the |Function| object "as a constructor"
appears and implies the |Function| object is not always a constructor.

Is definition 4.3.4 even correct?

"A constructor is a Function object that creates and initializes
objects. Each constructor has an associated prototype object that is
used to implement inheritance and shared properties."

When a |Function| object is called with |new|, is it the |Function|
object that creates the object that is then bound to |this| during the
execution of the function?

If the definition is correct then a function in a third-party library
cannot be termed a constructor or not by looking at the third-party
library alone. It would be necessary to look at the code using the
library to see if the function is every called with |new|.

Peter

dhtmlk...@gmail.com

unread,
Aug 2, 2007, 11:40:13 AM8/2/07
to
On Jul 31, 6:48 pm, David Mark <dmark.cins...@gmail.com> wrote:
> On Jul 31, 8:21 pm, "Richard Cornford" > > 1. b is a built-in object;

> not a String or string literal.
>
> [snip]

>
> > > Define b.
> > > if( b ) {
> > > alert( "if: " + typeof b );
> > > }
> > > else {
> > > alert( "else: " + b );
> > > }
> > > result: alerts "if: false"
>
> > Given the definition of "built-in object" in ECMA 262, 3rd Ed. Section
> > 4.3.7, and especially the words "Every built-in object is a native
> > object", and the definition of - typeof - operator in section 11.4.3,
> > where all 'native' objects must result in the strings 'object' or
> > 'function' when used as a operand of - typeof -, are you sure you are
> > not testing for knowledge of implementation bugs here?
>
> Seems like it. That would make it a silly question for a general
> JavaScript quiz though. Perhaps it is a trick question and the answer
> is "nothing at all."
>
No,

The answer could be: new Object("false"), new Object(false); Is this a
bug?

new Boolean( false ); will produce the same result in IE and in FF, I
think this is a bug.

Garrett


>
>
> > > 2. Define p and q (they are not Strings). Result:
> > > p < q; // false.

> > > p <= q; // true.


> > > p == q; // false.
>

> > What about the execution order of these tests, as a strict adherence to
> > the order shown greatly expands the possibilities?
>
> I don't understand what you mean by that. I assume the question meant
> to define the values once and then evaluate them in whatever order.
> Do you mean that you could re-assign p and q in between each test and
> come up with virtually infinite possibilities?


David Mark

unread,
Aug 2, 2007, 11:50:29 AM8/2/07
to
On Aug 2, 11:40 am, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
wrote:

> > Seems like it. That would make it a silly question for a general
> > JavaScript quiz though. Perhaps it is a trick question and the answer
> > is "nothing at all."
>
> No,
>
> The answer could be: new Object("false"), new Object(false); Is this a
> bug?
>

I don't see how either of those will work as answers for your first
question.


Henry

unread,
Aug 2, 2007, 12:39:27 PM8/2/07
to
On Aug 2, 4:22 pm, Peter Michaux wrote:
> On Aug 2, 4:11 am, Thomas 'PointedEars' Lahn wrote:
<snip>

>> That is not the definition of a constructor in ECMAScript, as
>> you can read in one of the previous chapters of ECMA-262.
>> So yes, the instruction was violated.
>
> True the definition of "constructor" in 4.3.4 indicates the
> instruction was violated; however, this definition is not
> consistent with the usage of the term "constructor" in the
> document. There are several places where calling the
> |Function| object "as a constructor" appears and implies
> the |Function| object is not always a constructor.
>
> Is definition 4.3.4 even correct?
>
> "A constructor is a Function object that creates and initializes
> objects. Each constructor has an associated prototype object that is
> used to implement inheritance and shared properties."

That is a definition that makes every single function (with the
exception of the build-in functions such as - parseInt - and - eval -
(because they don't have prototypes)) into constructors, which is
fine, and answers the question about Function.

> When a |Function| object is called with |new|, is it the |Function|
> object that creates the object that is then bound to |this| during
> the execution of the function?

It is the function's [[Construct]] method that has the object created.

> If the definition is correct then a function in a third-party
> library cannot be termed a constructor

<snip>

Yes it could, they all could, but it would probably would not be a
good idea to attach the label to any that were not intended to be use
with the - new - operator.

Thomas 'PointedEars' Lahn

unread,
Aug 2, 2007, 12:56:30 PM8/2/07
to
dhtmlk...@gmail.com wrote:
> David Mark wrote:
>> Richard Cornford wrote:

>>> dhtmlk...@gmail.com wrote:
>>>> 1. b is a built-in object;
>>>> not a String or string literal.
>>>> Define b.
>>>> if( b ) {
>>>> alert( "if: " + typeof b );
>>>> }
>>>> else {
>>>> alert( "else: " + b );
>>>> }
>>>> result: alerts "if: false"
>>> Given the definition of "built-in object" in ECMA 262, 3rd Ed. Section
>>> 4.3.7, and especially the words "Every built-in object is a native
>>> object", and the definition of - typeof - operator in section 11.4.3,
>>> where all 'native' objects must result in the strings 'object' or
>>> 'function' when used as a operand of - typeof -, are you sure you are
>>> not testing for knowledge of implementation bugs here?
>> Seems like it. That would make it a silly question for a general
>> JavaScript quiz though. Perhaps it is a trick question and the answer
>> is "nothing at all."
>
> No,
>
> The answer could be: new Object("false"), new Object(false); Is this a
> bug?
^
Why would that be an answer at all?

> new Boolean( false ); will produce the same result in IE and in FF,

Which is `object' (typeof Boolean(false)).

> I think this is a bug.

It's not a bug. There is a difference between Boolean objects and
primitive boolean values. Any valid object reference type-converts to
true, so the first branch is taken. Passing `false' to the Boolean
constructor does not change the fact that an object is created, and a
reference to it is returned.

ISTM you have misunderstood Richard. He meant that you expect the
typeof-Operation on a native object to yield "false", which would be an
implementation bug (or at least a language extension) if it occurred.

Peter Michaux

unread,
Aug 2, 2007, 1:07:44 PM8/2/07
to
On Aug 2, 9:39 am, Henry <rcornf...@raindrop.co.uk> wrote:
> On Aug 2, 4:22 pm, Peter Michaux wrote:
>
> > Is definition 4.3.4 even correct?
>
> > "A constructor is a Function object that creates and initializes
> > objects. Each constructor has an associated prototype object that is
> > used to implement inheritance and shared properties."
>
> That is a definition that makes every single function (with the
> exception of the build-in functions such as - parseInt - and - eval -
> (because they don't have prototypes)) into constructors, which is
> fine, and answers the question about Function.

If the definition is to make all functions "constructors" then it
should say something like "A constructor is a Function object that can
create...." The word "can" is missing and I read the definition to
mean "A constructor is a Function object that does create...." I think
the "creates" in the definition means that the function is called with
|new| at least once.

Peter

Peter Michaux

unread,
Aug 2, 2007, 1:25:51 PM8/2/07
to
On Aug 1, 5:41 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:

I did a little research and there were other language releases

http://browser.netscape.com/downloads/archive/

I downloaded the French release of v6.2 and the error message is in
English so presumably they didn't translate JavaScript errors in any
language release.

This wasn't a fresh install of windows so if there was some JavaScript
engine interferance from one version to another my results may be
meaningless. I will eventually make a fresh Windows install and
retest. Thanks for the tip, Richard.

Peter

Richard Cornford

unread,
Aug 2, 2007, 3:51:42 PM8/2/07
to
On Aug 2, 4:40 pm, dhtmlkitc...@gmail.com wrote:
> On Jul 31, 6:48 pm, David Mark <dmark.cins...@gmail.com> wrote:
>> On Jul 31, 8:21 pm, Richard Cornford
>>> On Aug 2, 4:40 pm, dhtmlkitc...@gmail.com wrote:
>>>> 1. b is a built-in object; not a String or string literal.
>
>>>> Define b.
>>>> if( b ) {
>>>> alert( "if: " + typeof b );
>>>> }
>>>> else {
>>>> alert( "else: " + b );
>>>> }
>>>> result: alerts "if: false"
>
>>> Given the definition of "built-in object" in ECMA 262, 3rd Ed.
>>> Section 4.3.7, and especially the words "Every built-in object
>>> is a native object", and the definition of - typeof - operator
>>> in section 11.4.3, where all 'native' objects must result in
>>> the strings 'object' or 'function' when used as a operand of
>>> - typeof -, are you sure you are not testing for knowledge of
>>> implementation bugs here?
>
>> Seems like it. That would make it a silly question for a general
>> JavaScript quiz though. Perhaps it is a trick question and the
>> answer is "nothing at all."
>
> No,
>
> The answer could be: new Object("false"), new Object(false);
> Is this a bug?

If the code - alert( "if: "+ typeof b ); - results in the alerting of
"if: false" when - b - is either of - new Object("false") - or - new
Object(false) - then that most definitely is a bug. The specified result
of the - typeof - operation would be the string "object".

In addition (and if I can be bothered to look-up the section numbers for
you the least you could do is read them) the detention of Built-in
objects reads:-

| A built-in object is any object supplied by an ECMAScript
| implementation, independent of the host environment, which
| is present at the start of the execution of an ECMAScript program.
| Standard built-in objects are defined in this specification, and an
| ECMAScript implementation may specify and define others. Every
| built-in object is a native object.

- and the results of both of - new Object("false") - and - new
Object(false) - fail to satisfy the condition of being "present at the
start of the execution of an ECMAScript program". So they are not
built-in objects.

It looks to me like your intended code would have been - alert( "if: "+
b ); - and you were trying to point out that the type-conversion to
string values of some objects, which are true (or have trueness) by
virtue of being objects, may still be the string "false".

It is a pity that all my talk of the - typeof - operator did not tip you
off that something must be up.

Now if you were looking for a built-in object that had these
characteristics the - Boolean.prototype - would satisfy that
requirement, being an instance of the Boolean object with a 'false'
value, and existing from the start of the execution of an ECMAScript
program.

Of course a question to which the answer was - Boolean.prototype - would
not come well from someone who objected to questions using - with - on
the grounds that they did not ever use - with -, because I have seen
people using - with - but I am yet to any real code contain any
references to - Boolean.prototype -.

> new Boolean( false ); will produce the same result in
> IE and in FF, I think this is a bug.

It is not a bug. The specification requires - new Object(false) - to be
the exact equivalent of - new Boolean(false) - and - new
Object("false") - to be the exact equivalent of - new String("false") -.
So if your "b is a ... ; not a String or string literal" where taken as
referring to String objects and string primitives then - new
Object("false") - would not be allowed anyway (the resulting value is a
String object).

Richard.

Richard Cornford

unread,
Aug 2, 2007, 3:49:45 PM8/2/07
to
Peter Michaux wrote:

> On Aug 2, 9:39 am, Henry wrote:
>> On Aug 2, 4:22 pm, Peter Michaux wrote:
>>
>>> Is definition 4.3.4 even correct?
>>
>>> "A constructor is a Function object that creates and initializes
>>> objects. Each constructor has an associated prototype object
>>> that is used to implement inheritance and shared properties."
>>
>> That is a definition that makes every single function (with the
>> exception of the build-in functions such as - parseInt - and
>> - eval - (because they don't have prototypes)) into
>> constructors, which is fine, and answers the question about
>> Function.
>
> If the definition is to make all functions "constructors" then
> it should say something like "A constructor is a Function object
> that can create...." The word "can" is missing and I read the
> definition to mean "A constructor is a Function object that
> does create...."

But function objects do create object, if you use them as the operand
for - new -.

> I think the "creates" in the definition means that the
> function is called with |new| at least once.

Wouldn't that become "has created"?

But take it however you like; all functions are constructors or no
functions are constructors, it remains the case that the heading for
section 15.3.2 reads "The Function Constructor", so there is a
'something' to which the specification attaches that label, and whether
the label is appropriate or not we all know what that 'something' is and
so can draw specific meaning from the term "the Function constructor".

Now I can understand quibbling about what constitutes a 'use' of the
Function constructor. For example, if you enumerated the properties of
the global object and acted upon the prototype of the first that was -
typeof - 'function' and had a name that began with 'F', would that be a
use of the function constructor? I would say yes, but the code would not
contain any - Function - identifier.

Richard.

Peter Michaux

unread,
Aug 2, 2007, 5:06:05 PM8/2/07
to
On Aug 2, 12:49 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:

> Peter Michaux wrote:
> > On Aug 2, 9:39 am, Henry wrote:
> >> On Aug 2, 4:22 pm, Peter Michaux wrote:
>
> >>> Is definition 4.3.4 even correct?
>
> >>> "A constructor is a Function object that creates and initializes
> >>> objects. Each constructor has an associated prototype object
> >>> that is used to implement inheritance and shared properties."
>
> >> That is a definition that makes every single function (with the
> >> exception of the build-in functions such as - parseInt - and
> >> - eval - (because they don't have prototypes)) into
> >> constructors, which is fine, and answers the question about
> >> Function.
>
> > If the definition is to make all functions "constructors" then
> > it should say something like "A constructor is a Function object
> > that can create...." The word "can" is missing and I read the
> > definition to mean "A constructor is a Function object that
> > does create...."
>
> But function objects do create object, if you use them as the operand
> for - new -.
>
> > I think the "creates" in the definition means that the
> > function is called with |new| at least once.
>
> Wouldn't that become "has created"?

If "has created" is the criterion, then in the following code |foo| is
not a constructor because this code has never been run.

function Foo(){}
var f = new Foo();

The phrase could be modified to be "after program execution has
created" or "during program execution does create". This would have to
be shored up for situations where "new Foo()" is written but never
executed.

> But take it however you like; all functions are constructors or no
> functions are constructors,

or the ones that are called with |new| are constructors and the others
aren't.

> it remains the case that the heading for
> section 15.3.2 reads "The Function Constructor", so there is a
> 'something' to which the specification attaches that label, and whether
> the label is appropriate or not we all know what that 'something' is and
> so can draw specific meaning from the term "the Function constructor".

agreed

> Now I can understand quibbling about what constitutes a 'use' of the
> Function constructor. For example, if you enumerated the properties of
> the global object and acted upon the prototype of the first that was -
> typeof - 'function' and had a name that began with 'F', would that be a
> use of the function constructor? I would say yes, but the code would not
> contain any - Function - identifier.

If writing Function.prototpye constitutes using the the "Function
constructor" then I would definitely agree with your yes.

Interesting stuff. I like the hand/fist analogy that a function is
only a constructor when called with "new". We don't need "constructor"
to be a synonym for "function". We already have "function" for that.

I suppose the folks that write ECMAScript specification didn't have
the centuries of work upon which the authors of Webster's Dictionary
could build (and people find problems with dictionary definitions all
the time anyway.)

Peter

Richard Cornford

unread,
Aug 2, 2007, 6:17:18 PM8/2/07
to
Peter Michaux wrote:
> On Aug 1, 4:04 pm, Richard Cornford wrote:
>>
>> The potential for extreme and long term harm that can
>> follow form employing the wrong person is such that
>> nobody gets in unless they really can do the job, or
>> clearly show the potential to learn the job very
>> quickly. The "best applicant" is rarely that person.
>
> Just out of curiosity

Don't get too curious, confidentially clauses apply.

> to which types of "extreme and
> long term harm" can result from JavaScript at your
> company?

Bankruptcy, unemployment for all the staff. We are in a business where
there are not that many potential customers world-wide so acquiring a
reputation for delivering faulty software would not be likely to go
unnoticed in our market place. Fortunately very little gets past our QA
department.

> When boiled down, most
> JavaScript jobs involve widgets and XHR requests.

Yes, if you wanted to boil it down that way that is my job precisely.

> I get the impression that you get to do something
> more risky/interesting.

Interesting has got to be very relative. I wouldn't do the job if it
wasn't interesting, but I do get to design at the architectural level.

Risky, on the other hand, is not my perception if my job. Worst-case I
get to work for someone else. There are at least big 3 city of London
banks at the moment struggling to find people to work on large "AJAX"
systems for them (and have been for the last couple of years).

I had a much greater perception of risk when I used to work in financial
services (server-side Java programming), where it was made very clear,
and often repeated, that there was no margin for getting things wrong at
all. And I often remember the incident were a single line of faulty code
resulted in an erroneous automated purchase of £48,000 of shares, cost
our client more then the programmer who wrote the line earned in a year
and was only forgiven by our management because the fault had been
spotted/corrected before it has cost anyone any 'real money'.

Richard.

Richard Cornford

unread,
Aug 2, 2007, 6:17:11 PM8/2/07
to
dhtmlk...@gmail.com wrote:
> On Jul 31, 4:43 pm, Richard Cornford wrote:
<snip>

>> /* unknown global code */
>> function outerFunction(){
>> /* unknown outer function body code */
>> function innerFunction(){
>> /* unknown inner function body code */
>> with(anObjectReference){
>> x = 5; //<--- The subject line of code.
>> }
>> /* more unknown inner function body code */
>> }
>> /* more unknown outer function body code */}
>>
>> /* more unknown global code */
>>
> I really do not like the 'with' statement.
>
> This question looks familiar.

It would be purely coincidental if it was.

> You don't by chance work at Google, richard?

There is no need to get abusive ;-)

<snip>


>> 5. The creation of an 'x' property of the object referred
>> to by 'anObjectReference' and the assignment of the
>> value 5 to that property.
>>
> This will confuse people who don't use 'with'

No, it may confuse the people who don't understand the - with -
statement. The people who do understand the - with - statement don't
(necessarily) use it but will not have a problem understanding its
implications.

> It's another reason I don't like 'with'; it looks like
> this might happen, but instead, the property won't be
> created on anObjectReference, but will be set, if it
> is found.

So that would be a 'not possible' then?

<snip>


>> Q2: If the line of code above is changed from - x = 5; -
>> to - var x = 5 - which (group of) the above are then the
>> possible outcomes of the execution of that line?
>>
> Another reason why 'with' should be avoided. It serves
> only to confuse.
>
> I think that:
> - the inner function will get a new local variable.
> - anObjectRef.x won't get updated

There seems to be a great deal of reluctance to go into detail on the
second question. To my mind the candidate's understanding of how the
possibilities change with the use of the - var - statement would be very
telling.

You have answered that #9 is possible and that #6 is not possible, but
your opinions on the rest are unstated.

> It would seem that x would be a new variable in a scope
> block, but with doesn't create a scope block, it instead
> conflates the current scope block.

"Scope block" is terminology unconnected with anything in javascript. As
such it can mean anything to anyone who reads it, or mean nothing at
all. With "conflate" meaning "to fuse; to combine into one; (literally)
to blow together" and applied to whatever a "scope block" is supposed to
be, there doesn't seem to much connection here with what a - with -
statements actually does and "conflates the current scope block". It all
sounds like VKesque BS to me, and like his probably belies a fundamental
misconception. It serves only to confuse.

>> I would have to go over the answers with the candidate taking
>> the test as there are a number of 'understandable mistakes' to
>> be easily made here (that is, getting some of them wrong is a
>> certain fail, but others may need the thinking behind the answer.)

<snip>


> I got an interview question just like this before.

Do we take it that was with Google? The quality of the javascript they
write would make that a surprising fact.

> The interviewer was focused on details of the with statement
> and scope and the cleverness of his question.

<snip>

So you didn't get the job then?

Richard.

Richard Cornford

unread,
Aug 2, 2007, 6:44:34 PM8/2/07
to
Peter Michaux wrote:
> On Aug 2, 12:49 pm, Richard Cornford wrote:
<snip>

> Interesting stuff. I like the hand/fist analogy that a
> function is only a constructor when called with "new". We
> don't need "constructor" to be a synonym for "function".
> We already have "function" for that.

From a conceptual point of view, paralleling class-based usage, the most
productive criteria for using the term 'constructor' is to apply it to
functions that are designed to be used to create instances (one or more)
of object with class-like similarities (what would be instances of a
class in a language that had more than one class of object).

The specification's definition is not helpful in practice because I
would argue that it includes almost all functions (is determined by an
object's having a [[construct]] method), and you argue that it would
depend on actual usage and so while unused no functions are
constructors. In either case 'function' is sufficient to state what the
object is, and the specification is precise about the behaviour so the
label is of nominal importance.

> I suppose the folks that write ECMAScript specification didn't
> have the centuries of work upon which the authors of Webster's
> Dictionary could build (and people find problems with dictionary
> definitions all the time anyway.)

The document may have benefited from the interest of a pedantic and
hyper-critical editor. Though given the delay experienced in the
proposed release of the 4th edition maybe it was better that the got
something implementable out of the door rather than enlessly argue about
the details.

Richard.

dhtmlk...@gmail.com

unread,
Aug 2, 2007, 7:16:39 PM8/2/07
to
On Aug 2, 9:56 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:

> dhtmlkitc...@gmail.com wrote:
> > David Mark wrote:
> >> Richard Cornford wrote:

Yeah, you're right.

> PointedEars
> --
> Prototype.js was written by people who don't know javascript for people
> who don't know javascript. People who don't know javascript are not the
> best source of advice on designing systems that use javascript.

> -- Richard Cornford, <f806at$ail$1$8300d...@news.demon.co.uk>


dhtmlk...@gmail.com

unread,
Aug 2, 2007, 7:19:09 PM8/2/07
to
On Aug 2, 12:51 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:

You're right.

> It looks to me like your intended code would have been - alert( "if: "+
> b ); - and you were trying to point out that the type-conversion to
> string values of some objects, which are true (or have trueness) by
> virtue of being objects, may still be the string "false".
>
> It is a pity that all my talk of the - typeof - operator did not tip you
> off that something must be up.
>

No need for the pity! I just didn't read that post yet. It's a lot to
keep up w/here. I'm in SF now, weather is good. I do go out
sometimes :D


> Now if you were looking for a built-in object that had these
> characteristics the - Boolean.prototype - would satisfy that
> requirement, being an instance of the Boolean object with a 'false'
> value, and existing from the start of the execution of an ECMAScript
> program.
>
> Of course a question to which the answer was - Boolean.prototype - would
> not come well from someone who objected to questions using - with - on
> the grounds that they did not ever use - with -, because I have seen
> people using - with - but I am yet to any real code contain any
> references to - Boolean.prototype -.
>

Cool! So that is the answer, and it does fulfill the 'built-in' req.
Sorry for leading you all astray with the Object constructor.

That was actually what I thought at first (Boolean object), but then
when I read the steps for evaluating an 'if' statement, I saw that it
called GetValue. I'm not understanding how GetValue works; does it try
to take the object as a variable from the containing scope? I don't
get it. Someone esplain, please.

I've debugged code like this. At a prominent company in Sunnyvale, I
found code that used the Boolean constructor in a conditional. It was
as if they wanted if(!!maybeUndefined), but instead used if( new
Boolean( maybeUndefined ) ). But definitely not a common case, you're
right.

I learned a lot about debugging others' code there. The Boolean thing
was just one small anomaly. Fortunately, they were pretty patient with
me and didn't take my code fixes too personally.

I've seen 'with' but only for simple examples, and not in any recent
code.

> > new Boolean( false ); will produce the same result in
> > IE and in FF, I think this is a bug.
>
> It is not a bug. The specification requires - new Object(false) - to be
> the exact equivalent of - new Boolean(false) - and - new
> Object("false") - to be the exact equivalent of - new String("false") -.
> So if your "b is a ... ; not a String or string literal" where taken as
> referring to String objects and string primitives then - new
> Object("false") - would not be allowed anyway (the resulting value is a
> String object).
>

Ah, that would explain why:
typeof new Object( "foo" );//Object
new Object( "foo" ).constructor // String

What a funny language.

> Richard.

Richard Cornford

unread,
Aug 2, 2007, 7:28:48 PM8/2/07
to
Richard Cornford wrote:
> dhtmlk...@gmail.com wrote:
<snip>

> <snip>
>>> 5. The creation of an 'x' property of the object referred
>>> to by 'anObjectReference' and the assignment of the
>>> value 5 to that property.
>>>
>> This will confuse people who don't use 'with'
>
> No, it may confuse the people who don't understand the - with -
> statement. The people who do understand the - with
> - statement don't (necessarily) use it but will not have
> a problem understanding its implications.
<snip>

And of course it is also the case that some (possibly the majority of)
of the people who do use the - with - statement do not understand what
it does. They may or may not experience confusion, but they will likely
be wrong regardless.

Richard.

Thomas 'PointedEars' Lahn

unread,
Aug 3, 2007, 2:32:43 AM8/3/07
to
[Trimmed quote, see
http://www.jibbering.com/faq/faq_notes/clj_posts.html and
http://netmeister.org/news/learn2quote.html]

dhtmlk...@gmail.com wrote:
> [...] "Richard Cornford" [...] wrote:


>> [...] dhtmlkitc...@gmail.com wrote:
>>> new Boolean( false ); will produce the same result in
>>> IE and in FF, I think this is a bug.
>> It is not a bug. The specification requires - new Object(false) - to be
>> the exact equivalent of - new Boolean(false) - and - new
>> Object("false") - to be the exact equivalent of - new String("false") -.
>> So if your "b is a ... ; not a String or string literal" where taken as
>> referring to String objects and string primitives then - new
>> Object("false") - would not be allowed anyway (the resulting value is a
>> String object).
>
> Ah, that would explain why:
> typeof new Object( "foo" );//Object
> new Object( "foo" ).constructor // String
>
> What a funny language.

The author of JavaScript himself is not quite happy with the type
distinction between primitive types like boolean and string, and object
types like Boolean and String:

http://weblogs.mozillazine.org/roadmap/archives/2005/11/js2.html


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

dhtmlk...@gmail.com

unread,
Aug 3, 2007, 3:17:34 AM8/3/07
to
On Aug 2, 11:32 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> [Trimmed quote, seehttp://www.jibbering.com/faq/faq_notes/clj_posts.htmlandhttp://netmeister.org/news/learn2quote.html]

>
>
>
> dhtmlkitc...@gmail.com wrote:
> > [...] "Richard Cornford" [...] wrote:
> >> [...] dhtmlkitc...@gmail.com wrote:
> >>> new Boolean( false ); will produce the same result in
> >>> IE and in FF, I think this is a bug.
> >> It is not a bug. The specification requires - new Object(false) - to be
> >> the exact equivalent of - new Boolean(false) - and - new
> >> Object("false") - to be the exact equivalent of - new String("false") -.
> >> So if your "b is a ... ; not a String or string literal" where taken as
> >> referring to String objects and string primitives then - new
> >> Object("false") - would not be allowed anyway (the resulting value is a
> >> String object).
>
> > Ah, that would explain why:
> > typeof new Object( "foo" );//Object
> > new Object( "foo" ).constructor // String
>
> > What a funny language.
>
> The author of JavaScript himself is not quite happy with the type
> distinction between primitive types like boolean and string, and object
> types like Boolean and String:
>
> http://weblogs.mozillazine.org/roadmap/archives/2005/11/js2.html
>
That's a lot to take in.

It appears that unboxing has been implemented in ES4.

Here's a console printout from ES4:

>> var b=new Boolean(false);
>> if( b ) print(b);
>> if( !b ) print(b);
false
>>

So it's new behavior, but I think it's a good thing; the current
behavior in es3 is misleading to developers.

Garrett

Richard Cornford

unread,
Aug 3, 2007, 3:49:52 AM8/3/07
to
dhtmlk...@gmail.com wrote:

> On Aug 2, 12:51 pm, Richard Cornford wrote:
>> On Aug 2, 4:40 pm, dhtmlkitc...@gmail.com wrote:
>>> On Jul 31, 6:48 pm, David Mark <dmark.cins...@gmail.com> wrote:
>>>> On Jul 31, 8:21 pm, Richard Cornford
<snip>

>>>>> Given the definition of "built-in object" in ECMA 262,
>>>>> 3rd Ed. Section 4.3.7, and especially the words "Every
>>>>> built-in object is a native object", and the definition
>>>>> of - typeof - operator in section 11.4.3, where all
>>>>> 'native' objects must result in the strings 'object'
>>>>> or 'function' when used as a operand of - typeof -,
>>>>> are you sure you are not testing for knowledge of
>>>>> implementation bugs here?
<snip>

>> It is a pity that all my talk of the - typeof - operator did
>> not tip you off that something must be up.
>>
> No need for the pity! I just didn't read that post yet. ...
<snip>

You have quoted the significant section of my post twice now in your own
posts. Not reading the material you quote is a reckless way of behaving
on Usenet.

<snip>


>> Now if you were looking for a built-in object that had
>> these characteristics the - Boolean.prototype - would
>> satisfy that requirement, being an instance of the Boolean
>> object with a 'false' value, and existing from the start of
>> the execution of an ECMAScript program.
>>
>> Of course a question to which the answer was - Boolean.prototype
>> - would not come well from someone who objected to questions
>> using - with - on the grounds that they did not ever use
>> - with -, because I have seen people using - with - but I am
>> yet to any real code contain any references to -
>> Boolean.prototype -.
>
> Cool! So that is the answer,

Not the answer to the question you actually asked. Rather the (but
possibly on an) answer to the question that you thought you had asked.

> and it does fulfill the 'built-in' req.
> Sorry for leading you all astray with the Object
> constructor.

And the use of - typeof - in the actual question.

> That was actually what I thought at first (Boolean object),
> but then when I read the steps for evaluating an 'if'
> statement, I saw that it called GetValue. I'm not
> understanding how GetValue works;

Section 8.7, and then section 8.6.2 for the [[Get]] method of objects.

The GetValue function turns instances of the internal Reference type
into the value of the property named by the "property name" of the
Reference type on the object referred to by the "base" of Reference
type. Whenever the argument to GetValue is not a Reference type the
argument itself is returned (unaltered).

Identifier resolution against the scope chain and the evaluation of
property accessors result in Reference type. The only other expression
that may result in a Reference type is a grouping operation, where the
parentheses of the grouping operator surrounding an Identifier or a
property accessor (only) (or a call to a host function/method, but there
is no evidence that any implementations have taken advantage of that
provision).

> does it try to take the
> object as a variable from the containing scope?

The wording of that question is too far detached form anything that has
meaning in javascript to be answered.

> I don't get it. Someone esplain, please.
>
> I've debugged code like this. At a prominent company in
> Sunnyvale,

As I recall VK claims to work (or have worked) in Sunnyvale. There must
be quite a trail of buggy and irrational code following him about.

> I found code that used the Boolean constructor in a conditional.

It was the use of - Boolean.prototype - that I observed that I had never
seen in practice. The Boolean constructor is often used (called as a
function) to do type-conversion to boolean.

> It was
> as if they wanted if(!!maybeUndefined),

That would be redundant as the type-conversion to boolean is implied in
the evaluation of the - if - expression (step 3 in both of the
algorithms of Section 12.5). You could just write - if(
maybeUndefined ){ ... } - as if the Identifier is undeclared in addition
to its value possibly being undefined then all these permutations would
error out anyway.

> but instead used if( new Boolean( maybeUndefined ) ).
> But definitely not a common case, you're right.

I can see how that might happen (assuming VK was not responsible as that
is the type of thing he would do anyway).

Bollean( x)

- is guaranteed to have the same result as:-

!!x

- and is proposed to be superior (if slower) because it is
self-documenting (that is, states at the point of forcing the
type-conversation what type is expected to be the outcome). But if you
expose - Boolean(x) - to JSLint it will flag the failure to use the -
new - operator with the Boolean constructor (Douglas prefers !!x for
type conversion). So correcting that without thinking about what was
happening might turn a reasonable, but redundant, - if( Boolean( x )){
... } - into a faulty - if( new Boolean( x )){ ... } -.

> I learned a lot about debugging others' code there. The Boolean
> thing was just one small anomaly. Fortunately, they were pretty
> patient with me and didn't take my code fixes too personally.
>
> I've seen 'with' but only for simple examples, and not in any
> recent code.

In my opinion there is only one valid use for - with - (explicit scope
chain augmentation of function expressions), but the situations where
that is either necessary or even advantageous are so few and far between
that I would not expect to see it used more than once in maybe 50,000
lines of code (assuming knowledgeable authors of that code).

>>> new Boolean( false ); will produce the same result in
>>> IE and in FF, I think this is a bug.
>>
>> It is not a bug. The specification requires - new Object(false)
>> - to be the exact equivalent of - new Boolean(false) - and
>> - new Object("false") - to be the exact equivalent of -

>> new String("false") -. ...
<snip>


>
> Ah, that would explain why:
> typeof new Object( "foo" );//Object
> new Object( "foo" ).constructor // String

Yes it would.

> What a funny language.

But sharing with all programming languages the characteristics of being
completely logical and predictable, and so understandable.

Richard.

Richard Cornford

unread,
Aug 4, 2007, 12:45:47 PM8/4/07
to
Richard Cornford wrote:
<snip>

> "Inspired" by one of the more ambiguous questions on your
> meebo.com page I thought the following might make quite
> interesting written test questions, and give an impression
> of my thought process in setting javascript questions:-
>
> /* unknown global code */
> function outerFunction(){
> /* unknown outer function body code */
> function innerFunction(){
> /* unknown inner function body code */
> with(anObjectReference){
> x = 5; //<--- The subject line of code.
> }
> /* more unknown inner function body code */
> }
> /* more unknown outer function body code */
> }
> /* more unknown global code */
>
> /* ********************************************************\
> | Note: Three facts about the 'unknown' code:- |
> | |
> | 1. There are no more function definitions, no function |
> | expressions and no uses of the Function constructor. |
> | 2. There are no - with - statements in the unknown code.|
> | 3. There are no uses of the - eval - function. |
> \******************************************************** */
>
> Q1: Assuming the line that reads - x = 5; - is executed, which
> (group of) of the following are possible outcomes of its execution?

[My second question, quoted out of sequence:-]


| Q2: If the line of code above is changed from - x = 5; - to -
| var x = 5 - which (group of) the above are then the possible
| outcomes of the execution of that line?

At the point where the line of code is executed the - with - statement
has augmented the scope chain by adding an object to the top (or front,
depending on how you want to look at it) of it. Giving a scope chain
consisting of 4 objects; the object added with the - with - statement,
the Activation/Variable object from the execution context in which the -
x = 5; - line is executing, the Activation/Variable object associated
with the outer function when the inner function object was created, and
finally the global object.

The unknowns in this code allow any object to be the one added at the
top of the scope chain, and for any local variables to have been
declared in any function or globally, and so result in either
Activation/Variable object and the global object having any properties
when the - x = 5; - line is executed.

The procedure for resolving Identifiers such as - x - is to start at the
top of the scope chain and examine the object there to see if it has a
property with a name that corresponds with the Identifier. The nature of
this examination is to call the object's internal [[HasPropery]] method,
which, in the even that it cannot find a property on the object itself,
calls the [[HasProperty]] method of the object that is referred to by
its [[Prototype]] (if it is not null). Thus the test to see if the
object has a property with the given name is applied to the object and
all the objects on its prototype chain.

If the first object on the prototype chain does not have a property with
the corresponding name the testing moves to the next object in the
chain, and the test is repeated on that object, and so on until either
an object with a property with the corresponding name is found or the
scope chain comes to an end and there are no more objects to examine.

If an object on the scope chain (or one of its prototypes) is found to
have a property with the corresponding name the result of the resolution
of the Identifier is a Reference type with its 'base' property set to a
reference to the object on the scope chain, and its 'propertyName'
property set to the name of the property found (that is, the
Identifier).

If no object on the scope chain (or any of their prototypes) was found
to have a property with the corresponding name the result is again a
Reference type with its 'propertyName' property set to name that
corresponds with the Identifier, but this time the 'base' property is
set to null.

An assignment involves evaluating the expression to be assigned (to the
numeric value 5 in our case) and then resolving the left hand side of
the assignment into a Reference type (or generating a runtime error if
that is not possible) and then assigning the value of the right hand
expression to a property of the object referred to by 'base' property of
the Reference type with the name that is the Reference type's
'propertyName' value. If the Reference type's 'base' property is null
the global object becomes the substitute and it receives the assigned
value in one of its properties. And whenever the object (itself, not its
prototypes) does not already have a property with the corresponding name
one is created on it.

Thus the type of outcomes that are possible with - x = 5; - and - var x
= 5; - are the assignment of the value 5 to properties of objects and a
possible side effect of creating new properties of (some of) those
objects as a consequence. The subjects of the individual answers are
then which of these actions could possibly be applied to the various
objects that are of interest here, in the context of the code (both
specified and possible).

A quick listing of my answers, where 'P' indicates a 'Possible' and 'N'
a 'Not Possible':-

| x = 5 | var x = 5
----|---------|-----------
1 | P | P
2 | P | P
3 | P | P
4 | P | P
5 | P | P
6 | P | P
7 | N | N
8 | P | N <<- Changed by adding - var -.
9 | N | N
10 | P | P
11 | N | N
12 | P | P
13 | P | P
14 | P | P
15 | P | P
16 | P | P
17 | P | P
--------------------------

Some answers can be regarded as, as RobG suggested, splitting hairs, and
others may seem that way. However, with the exception of items such as
#7, where getting it wrong would be fatal, coming up with an answer that
differs from mine is neither unacceptable nor unexpected. My interest is
more in how the individuals react to being shown why they were wrong.

#1, #3 and #5 being examples of this. The question hangs on the
interpretation of the exact wording (the subject is always the object
itself not its prototypes) so it is very easy to miss the distinction
between the object as you would interact with it in code (where it
behaves as if it has all the properties of its prototype) and the object
as it really is. If someone says that the answers are 'not possible'
because they perceive that and object inheriting a property from a
prototype is equivalent to its having that property then that is a
viable and acceptable way of looking at the situation. My interest would
be in seeing whether, being shown how #1 is possible, they perceived the
implications for #3 and #5 immediately (and #13 and #15 in the - var x
= 5; - case, but more on that later).

(Though all credit to those who do spot the possibility, and so -
zeroglif@gmai ... - who posted the demonstration of the possibility
within a minute of my posting that there was a proof of the possibility,
and so independently of my post implying that looking for a proof might
be worth the effort).

So to the answers:-

> 1. The creation of an 'x' property of the 'outerFunction'
> function and the assignment of the value 5 to that property.

x = 5; - As has been discussed, 'outerFunction' is a discrete object
that inherits properties from its prototypes (Function.prototype and
Object.prototype). If the - with - statement is used to put a reference
to it at the top of the scope chain the testing of that object to see if
it 'has' an 'x' property, using its internal [[HasProperty]] method,
will return true if the property is defined on either prototype but not
the object itself. Then the assignment would target the 'outerFunction'
object and create a property on that object as a side effect:-

Object.prototype.x = 0;

function outerFunction() {
function innerFunction() {
var anObjectReference = outerFunction;
alert(outerFunction.hasOwnProperty('x')); //false
with (anObjectReference) {
x = 7;
}
alert(outerFunction.hasOwnProperty('x'));//true
}
innerFunction();
}
outerFunction();

The - hasOwnProperty - method being a good indicator of whether the
object itself has the property in question.

#1 is possible.

var x = 5; - The situation is not altered by declaring a function local
variable for 'innerFunction' because the Variable object for
'innerFunction' is below whatever object the - with - statement added to
the scope chain and so when that added object becomes the target for the
assignment the scope chain resolution of the 'x' Identifier stops and so
never gets to innerFunctios' Variable object. #1 is possible.

> 2. The assignment of the value 5 to a pre-existing 'x' property
> of the 'outerFunction' function.

x = 5; - If 'outerFunction' has an 'x' property and it is placed at the
top of the sc0pe chain by the - with - statement the assignment is going
to target that property of that object. #2 is possible.

var x = 5; - Again the situation is not altered by declaring a function
local variable for 'innerFunction'. #2 is possible.

> 3. The creation of an 'x' property of the 'innerFunction'
> function and the assignment of the value 5 to that property.

x = 5; - 'innerFunction' and its prototypes can be subject to exactly
the same manipulations as 'outerFunction' and it can also be placed at
the top of the scope chain. #3 is possible.

var x = 5; - The situation is not altered by declaring a function local
variable for 'innerFunction'. #3 is possible.

> 4. The assignment of the value 5 to a pre-existing 'x' property
> of the 'innerFunction' function.

x = 5; - Whatever is possible for 'outerFunction' is possible for
'innerFunction'. #4 is possible.

var x = 5; - The situation is not altered by declaring a function local
variable for 'innerFunction'. #4 is possible.

> 5. The creation of an 'x' property of the object referred to by


> 'anObjectReference' and the assignment of the value 5 to that
> property.

x = 5; - if something is possible for 'outerFunction' as a result of
making it "the object referred to by 'anObjectReference'" it must also
be possible for the object referred to by 'anObjectReference'. #5 is
possible.

var x = 5; - The situation is not altered by declaring a function local
variable for 'innerFunction'. #5 is possible.

> 6. The assignment of the value 5 to a pre-existing 'x'
> property of the object referred to by 'anObjectReference'.

x = 5; Same as #5. #6 is possible.

var x = 5; - The situation is not altered by declaring a function local
variable for 'innerFunction'. #6 is possible.

> 7. The creation of a local variable of the 'outerFunction'

> function named 'x' and the assignment of the value 5 to
> that variable.

x = 5; An assignment does not declare a variable. #7 is not possible
(getting this one wrong is fatal).

var x = 5; - Declaring a function local variable for 'innerFunction' is
not declaring a function local variable for 'outerFunction' (and that is
assuming that "the execution of that line" could result in "the
creation of a local variable" at all). #7 is not possible (getting this
one wrong is fatal).

> 8. The assignment of the value 5 to a declared local variable
> of the 'outerFunction' function named 'x'.

x = 5; Yes, if 'outerFunction' included a declaration of a local
variable 'x' and no object higher on the scope chain had an 'x' property
then the assignment of the value 5 to that local variable is the
expected outcome. #8 is possible (getting this one wrong is fatal).

var x = 5; - Declaring a function local variable for 'innerFunction'
results in its Variable object having an 'x' property, and its variable
object is above that of 'outerFunction' on the scope chain so the
Identifier resolution can never get to the Variable object for
'outerFunction'. (it is not possible to reference any Variable object
except the global object, so the - with - statement cannot be used to
put the 'outerFunction' Variable object at the top of the scope chain.)
#8 is not possible (getting this one wrong is fatial).

> 9. The creation of a local variable of the 'innerFunction'

> function named 'x' and the assignment of the value 5 to
> that variable.

x = 5; An assignment does not declare a variable. #9 is not possible
(getting this one wrong is fatal).

var x = 5; - Strictly this is not possible because the questions ask
about the "outcomes of the execution of that line" and the local
variable that results for the variable declaration here was created
during variable instantiation for the execution context. That is, the
variable already exists at that time of the execution of the line, and
so cannot be created at that time.

The existence of the line - var x = 5; - does result in "The creation of
a local variable of the 'innerFunction' function named 'x'" and may
result in "the assignment of the value 5 to that variable" but the
_execution_ of the line does not. #9 is not possible.

> 10. The assignment of the value 5 to a declared local variable
> of the 'innerFunction' function named 'x'.

x = 5; This is entirely possible. #10 is possible (getting this one
wrong is fatal).

var x = 5; - This is entirely possible, and you don't need any other
variable declarations for 'x' (even though they are allowed (not
encouraged)) because, as I mentioned for #9, the variable declaration
here has created a local variable during variable instantiation, and so
at the time of executing the line a local variable of the
'innerFunction' is certain to be in existence. #10 is possible (getting
this one wrong is fatal).

> 11. The creation of a global variable named 'x' and the
> assignment of the value 5 to that variable.

x = 5; Here is the hair-splitting quibble; The declaration of a
variable results in the creation of a property of an object (a Variable
object) and the declaration of a global variable results in the creation
of a property of the global object (because the global object is used as
the Variable object for variable instantiation in the global execution
context). And if no object on the scope chain is found to have an 'x'
property then the result of the assignment is the creation of an 'x'
property of the global object, which certainly is also a consequence of
declaring a global variable.

However, there are two reasons for not considering the runtime creation
of a property of the global object as being "the creation of a global
variable". The first, and most arguable, is timing; variables are
created during variable instantiation and prior to the execution of any
code for the execution context in question. The second is that
properties of objects created during variable instantiation in function
and global execution contexts are marked with the - DontDelete -
attribute. Thus a global variable cannot be deleted with the - delete -
operator, a runtime-created property of the global object can be deleted
so it is not a global variable. #11 is not possible. (Quibbling about
variable instantiation in - eval - code execution contexts remains a
possibility. And a candidate taking that line would act to impress
(possibly just for their knowing the specifics of the eval case)).

var x = 5; - Not possible, but remember that it is possible to put the
global object at the top of the scope chain, and if you can get at the
prototype for the global object (if it has one at all) then the
technique used in #1 can be applied to create a property of the global
object with the execution of this line of code. So it is a good thing
that creating a property of the global object alone could not be
considered as "the creation of a global variable". #11 is not possible.

> 12. The assignment of the value 5 to a declared global
> variable named 'x'.

x = 5; - #12 is possible (getting this wrong is fatal).

var x = 5; If the global object is the one placed at the top of the
scope chain by the - with - statement, and it has a global variable
named 'x' then the execution of this line of code will assign the value
5 to that global variable. #12 is possible.

> 13. The creation of an 'x' property of the global object and the


> assignment of the value 5 to that property.

x = 5; - #13 is possible (getting this wrong is fatal).

var x = 5; ECMA 262 3rd Ed. is unspecific on the subject of the
prototype of the global object. It allows that the global object may
have no prototype at all, but leaves it to the implementation to decide
how to handle the situation. Now suppose the global object inherits
from - Object.prototype -, which is hardly an unexpected thing for an
object in javascript to do, then the technique used in #1 can be applied
here to create a property of the global object at runtime. #13 is
possible.

> 14. The assignment of the value 5 to a pre-existing 'x' property
> of the global object.

x = 5; - #14 is possible (getting this wrong is fatal).

var x = 5; If the global object is the one placed at the top of the
scope chain by the - with - statement, and it has an 'x' property then
the execution of this line of code will assign the value 5 to that 'x'
property of the global object. #14 is possible.

> 15. The creation of an 'x' property of the window object and


> the assignment of the value 5 to that property.

x = 5; - There is no requirement that there be a window object at all,
and in WSH and ASP, for example, there is no window object (the concept
has no meaning in host contexts), but in a web browser environment the
window object is expected to be the same object as the global object
(and expectation that can be observed as being rewarded in actual
browsers). There is nothing in the questions that preclude the code from
being executed in a web browser where the ECMAScript global object is
the window object, and so #15 is possible.

var x = 5; - if the window object and the global object are the same
object then whatever can be done with the global object can also be done
with the window object, so as #13 is possible #15 is possible.

> 16. The assignment of the value 5 to a pre-existing 'x' property
> of the window object.

x = 5; - If the window object and the global object are the same object
then whatever can be done with the global object can also be done with
the window object, so as #14 is possible #16 is possible.

var x = 5; - If the window object and the global object are the same
object then whatever can be done with the global object can also be done
with the window object, so as #14 is possible #16 is possible.

> 17. A runtime error.
<snip>

x = 5; - Disregarding possible sources of errors on the preceding line
(or in other places in the code) which may prevent the subject line of
code being executed (and so are precluded by the presupposition that it
is executed implied by the question), it has been demonstrated that this
line can be made to error in environments as common as IE 6. #17 is
possible.

var x = 5; - If, in IE 6, you create the conditions that have been
described to make - x = 5; - error, and then place the window/global
object at the top of the scope chain with the - with - statement, then
you get the same error with - var x = 5; -. #17 is possible.

------------------------------------------------------

Given the number of seriously counter-intuitive possibilities introduced
by the use of the - with -, particularly things like - var x = 5; -
occurring nested two functions deep can still set the value of a global
variable, I hope that leaves nobody in any doubt as to why the use of
the - with - statement is _strongly_ discouraged in production
javascript. But still, if you have applied for a job as a javascript
expert is there an excuse for not knowing what the - with - statement
does, or not understand code that does use it?

It should also be seen that the strict observance of many other 'best
practice' notions would impact on these questions. Such as 'if you are
using something that is conceptually a global variable it should always
be explicitly declared as a global variable'. Which precludes the stated
IE 6 runtime errors, any questions of playing around with the prototype
of the global object and any hair splitting about whether a property of
the global object is a variable or not. And the suggestion that
function local variables should all be explicitly declared at the start
of a function body (so in a context that mirrors the order of their
handling, i.e. declarations being handled during variable instantiation,
prior to the actual execution of any function body code) guarantees that
they cannot then appear inside a Block Statement, and so cannot appear
where they may be (or appear to be) influenced by a scope chain
augmented by the - with - statement.

Then again, there were also counter-intuitive possibilities introduced
by prototype inheritance, which is pretty fundamental to what javascript
is.

Going back to my inspiration for the question, the meebo.com page's
question:-

| 2. What's the difference between these two statements:
|
| a. var x = 3;
|
| b. x = 3;

- (Remembering that the only answer to the questions above that changed
as a result of using - var - was the answer to #8.) does anyone still
think it can be answered at all? Or answered concisely but not
trivially? Or that the person setting that question would see any
accurate answer given as an indication of the expertise they were
looking for?

Richard.

Zeroglif

unread,
Aug 4, 2007, 8:14:40 PM8/4/07
to
Thanks Richard, that was really interesting. Any plans for new test?

Richard Cornford

unread,
Aug 5, 2007, 9:27:51 AM8/5/07
to
Zeroglif wrote:
> Thanks Richard, that was really interesting.
> Any plans for new test?

No plans, and certainly not in the short term. You have to remember that
I have a practical use for these questions and posting that question
denies me the possibility of using it myself.

On the other hand I liked RobG's suggestion in response to #17 that a
question could be directed at runtime errors. You could start off with a
set of conditions such as:-

1. The environment is a (hypothetical) scriptable HTML web
browser.
2. The script implementation is 100% ECMA 262 3rd Ed. conforming.
3. The ECMAScript global object has a - window - property that
is a reference to the ECMAScript global object.
4. The ECMAScript global object has a - document - property that
is a reference to an object.
5. There are no uses of the - with - statement, the - eval -
function or the - Function - constructor anywhere on the page.
6. The code shown never appears in HTML attribute values (unless
explicitly stated).

- and probably some others that I have not yet thought of. (it might be
worth saying that the object referred to by - document - implements ECMA
262 conforming [[Get]] and [[Put]] methods, and maybe that chunks of
pre-DOM are implemented; location, history, frames, forms, links,
anchors, etc.)

Ask the question "Which of the following can never result in a runtime
error?", and provide a list of blocks of code (possibly, and
particularly in the case of function definitions, including details of
how the code is used/called).

And so see how easily people perceive the boundary between what is
'safe' and what is not (and some other things such as putting line that
obviously may error inside and - if - were the condition can never be
true, and so see if they observe that there cannot be an error because
the error producing line cannot be executed).

Richard.

0 new messages