473,503 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JavaScript knowledge test

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

Jul 31 '07 #1
60 4928
rf

"marss" <ma******@gmail.comwrote in message
news:11**********************@l70g2000hse.googlegr oups.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 :-)
js_quiz.asp), but I need more.

Thanks,
Mykola

Jul 31 '07 #2
On 31 , 15:57, "rf" <r...@invalid.comwrote:
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") :(

Jul 31 '07 #3
On 31 , 16:02, Henry <rcornf...@raindrop.co.ukwrote:
>
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.

Jul 31 '07 #4
On Jul 31, 5:24 am, marss <marss...@gmail.comwrote:
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

Jul 31 '07 #5
On Jul 31, 8:29 am, Peter Michaux <petermich...@gmail.comwrote:
On Jul 31, 5:24 am, marss <marss...@gmail.comwrote:
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"

2. Define p and q (they are not Strings). Result:
p < q; // false.
p <= q; // true.
p == q; // false.
Jul 31 '07 #6
On Jul 31, 6:56 pm, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
wrote:
On Jul 31, 8:29 am, Peter Michaux <petermich...@gmail.comwrote:
On Jul 31, 5:24 am, marss <marss...@gmail.comwrote:
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;

Jul 31 '07 #7
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.)

Aug 1 '07 #8
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/
Aug 1 '07 #9
On Aug 1, 12:15 am, Peter Michaux <petermich...@gmail.comwrote:
[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."

Aug 1 '07 #10
On 31 , 18:29, Peter Michaux <petermich...@gmail.comwrote:
>
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 :)

Aug 1 '07 #11
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.
Aug 1 '07 #12
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

Aug 1 '07 #13
On Aug 1, 5:32 pm, marss <marss...@gmail.comwrote:
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.

Aug 1 '07 #14
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.auwrote:
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

Aug 1 '07 #15
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.

Aug 1 '07 #16
Richard Cornford 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. |
\************************************************* ******* */
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.
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
Aug 1 '07 #17
On Aug 2, 2:56 am, Peter Michaux <petermich...@gmail.comwrote:
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.auwrote:
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

Aug 1 '07 #18
RobG wrote:
On Aug 2, 2:56 am, Peter Michaux <petermich...@gmail.comwrote:
>On Aug 1, 6:50 am, RobG <rg...@iinet.net.auwrote:
>>>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.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Aug 1 '07 #19
On Aug 1, 2:04 pm, RobG <rg...@iinet.net.auwrote:
>
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

Aug 1 '07 #20
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.comwrote:
On Aug 1, 6:50 am, RobG <rg...@iinet.net.auwrote:
>>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();

Aug 1 '07 #21
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.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Aug 1 '07 #22
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.

Aug 1 '07 #23
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.

Aug 1 '07 #24
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.

Aug 1 '07 #25
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.

Aug 1 '07 #26
On Aug 1, 7:18 pm, David Mark <dmark.cins...@gmail.comwrote:
>
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?

Aug 1 '07 #27
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();

Aug 1 '07 #28
On Aug 1, 4:28 pm, David Mark <dmark.cins...@gmail.comwrote:
On Aug 1, 7:18 pm, David Mark <dmark.cins...@gmail.comwrote:
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.
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();
Most of the other posters had similar takes. What are the correct
answers as you see them?

Aug 1 '07 #29
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.

Aug 1 '07 #30
"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.

Aug 1 '07 #31
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.

Aug 1 '07 #32
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

Aug 1 '07 #33
On Aug 1, 7:38 pm, Peter Michaux <petermich...@gmail.comwrote:
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.

Aug 1 '07 #34
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

Aug 2 '07 #35
On Aug 1, 4:36 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
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.
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

Aug 2 '07 #36
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.

Aug 2 '07 #37
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.

Aug 2 '07 #38
On Aug 1, 7:36 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
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 =
Doh! That is surprising.

Aug 2 '07 #39
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.

Aug 2 '07 #40
Peter Michaux wrote:
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.
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.

Aug 2 '07 #41
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.

Aug 2 '07 #42
On Aug 1, 8:18 pm, Peter Michaux <petermich...@gmail.comwrote:
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);
with (anObjectReference) {
x = 7;
}
alert(innerFunction.x);
}
innerFunction();

}

outerFunction();
Aug 2 '07 #43
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.

Aug 2 '07 #44
On Aug 1, 5:41 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
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.
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

Aug 2 '07 #45
On Jul 31, 4:53 pm, David Mark <dmark.cins...@gmail.comwrote:
On Jul 31, 6:56 pm, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
wrote:
On Jul 31, 8:29 am, Peter Michaux <petermich...@gmail.comwrote:
On Jul 31, 5:24 am, marss <marss...@gmail.comwrote:
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.

Aug 2 '07 #46
On Jul 31, 4:53 pm, David Mark <dmark.cins...@gmail.comwrote:
On Jul 31, 6:56 pm, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
wrote:
On Jul 31, 8:29 am, Peter Michaux <petermich...@gmail.comwrote:
On Jul 31, 5:24 am, marss <marss...@gmail.comwrote:
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);


Aug 2 '07 #47
On Aug 2, 2:41 am, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
wrote:
On Jul 31, 4:53 pm, David Mark <dmark.cins...@gmail.comwrote:


On Jul 31, 6:56 pm, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
wrote:
On Jul 31, 8:29 am, Peter Michaux <petermich...@gmail.comwrote:
On Jul 31, 5:24 am, marss <marss...@gmail.comwrote:
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.

Aug 2 '07 #48
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.
Aug 2 '07 #49
dh**********@gmail.com wrote:
David Mark wrote:
>Richard Cornford wrote:
>>dh**********@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.
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, <f8*******************@news.demon.co.uk>
Aug 2 '07 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
4181
by: binnyva | last post by:
Hello Everyone, I have just compleated a JavaScript tutorial and publishing the draft(or the beta version, as I like to call it) for review. This is not open to public yet. The Tutorial is...
136
9201
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.