JavaScript knowledge test

R

Richard Cornford

Peter Michaux wrote:
// 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.
 
P

Peter Michaux

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();
 
P

Peter Michaux

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
 
D

David Mark

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

Peter Michaux

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
 
P

Peter Michaux

Peter Michaux wrote:

// 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
 
D

David Mark

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();

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

Richard Cornford

David said:
On Aug 1, 7:18 pm, David Mark wrote:
... . 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.
 
R

Richard Cornford

David said:
On Aug 1, 7:04 pm, Richard Cornford wrote:

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

David Mark

Thomas said:
Peter said:
| 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.
 
D

David Mark

On Aug 1, 8:06 pm, "Richard Cornford" <[email protected]>
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.
 
P

Peter Michaux

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

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
 
R

Richard Cornford

Peter said:
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.
 
Z

zeroglif

On Aug 1, 7:35 pm, (e-mail address removed) wrote:
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.

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

David Mark

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.)
The question is the assessment of which are possible and which are not.

Yep. I missed that entirely on the first try.
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).




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


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.


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

Richard Cornford

Peter said:
Peter Michaux wrote:
// 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? ..
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.
 
D

David Mark

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();
 
P

Peter Michaux

Peter said:
Peter Michaux wrote:
// 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? ..
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
 
D

dhtmlkitchen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top