Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Are you a javascript expert? Test

Reply
Thread Tools

Are you a javascript expert? Test

 
 
optimistx
Guest
Posts: n/a
 
      10-26-2009
What does the alert statement in these 5 cases output, exactly?Of course,
the test should be done without debugger, without executing,without googling
, with honesty ...1)if (!("a" in window)) {
var a = 1;
}
alert(a);2)var a = 1,
b = function a(x) {
x && a(--x);
};
alert(a);3)
function a(x) {
return x * 2;
}
var a;
alert(a);4)
function b(x, y, a) {
arguments[2] = 10;
alert(a);
}
b(1, 2, 3);5)
function a() {
alert(this);
}
a.call(null);The code was copied (without doublechecking)
fromhttp://dmitry.baranovskiy.com/

 
Reply With Quote
 
 
 
 
rf
Guest
Posts: n/a
 
      10-26-2009

"optimistx" <(E-Mail Removed)> wrote in message
news:4ae57a79$0$26343$(E-Mail Removed)...
> What

bloody atrocious layout.


 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      10-26-2009
On Oct 26, 10:31 am, optimistxwrote:
> What does the alert statement in these 5 cases output, exactly?
> Of course, the test should be done without debugger, without
> executing,without googling , with honesty ...


And presumably with the assumption that each single example is in
isolation. And preferably with some effort having been put in with
regard to providing a readable presentation.

> 1)if (!("a" in window)) {
> var a = 1;}
>
> alert(a);


The expected output is 'undefined', as the - a - variable is declared
but never assigned a value (because being declared the - "a" in window
- is true, at least it would have been guaranteed to be true of - "a"
in this - had been used, the window <-> ECMAScript global object
relationship being a little more involved).

> 2)var a = 1,
> b = function a(x) {
> x && a(--x);
> };
> alert(a);


The expected output is '1'. The JScript named function expression bug
came into play but the assignment of 1 to - a - happened after
variable instantiation so the reference to the extra function has been
replaced.

> 3)
> function a(x) {
> return x * 2;}
>
> var a;
> alert(a);


The expected output is 'function a(x)( return x * 2;}', but possibly
with formatting.

> 4)
> function b(x, y, a) {
> arguments[2] = 10;
> alert(a);}
>
> b(1, 2, 3);


The expected output is '10'.

> 5)
> function a() {
> alert(this);}
>
> a.call(null);


The expected output is the string representation of the global object,
which is implementation dependent.

> The code was copied (without doublechecking)


Or much thought going into presentation.

> fromhttp://dmitry.baranovskiy.com/


Richard.
 
Reply With Quote
 
Garrett Smith
Guest
Posts: n/a
 
      10-26-2009
Richard Cornford wrote:
> On Oct 26, 10:31 am, optimistxwrote:


[...]
>> 4)
>> function b(x, y, a) {
>> arguments[2] = 10;
>> alert(a);}
>>
>> b(1, 2, 3);

>
> The expected output is '10'.


Watch out for old versions Safari <= 2.0 (elerts "3").
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
 
Reply With Quote
 
Dmitry A. Soshnikov
Guest
Posts: n/a
 
      10-26-2009
On Oct 26, 2:18*pm, Richard Cornford <(E-Mail Removed)>
wrote:

> [...]
> The JScript named function expression bug
> came into play but the assignment of 1 to - a - happened after
> variable instantiation so the reference to the extra function has been
> replaced.
>


JScript NFE's bug doesn't matter in here, anyway result will be [1] -
with correct implementation of NFE or not.


> [...]
> The expected output is the string representation of the global object,
> which is implementation dependent.
>


Yeah, but e.g. Google Chrome seems to distinguish real [undefined]
value and [not passed] parameter:

var getClass = Object.prototype.toString;

alert(getClass.call()); // [object Object]
alert(getClass.call(undefined)); // [object builtins]
alert(getClass.call(null)); // [object builtins]
alert(getClass.call(this)); // [object global]

First three calls should use the global object as [thisArg] (by
15.3.4.4, ES-3), and the fours one - is real global object passed (in
code of the global context), but as we can see Chrome treats this
cases differently.

/ds
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      10-26-2009
On Oct 26, 11:31*am, "optimistx" <(E-Mail Removed)> wrote:
> What does the alert statement in these 5 cases output, exactly ?
> Of course,
> the test should be done without debugger, without executing,
> without googling, with honesty ...


function f () { return 1; }

if (true) {
function f () { return 2; }
} else {
function f () { return 3; }
}

alert(f()); // ?
--
Jorge.
 
Reply With Quote
 
Dmitry A. Soshnikov
Guest
Posts: n/a
 
      10-26-2009
On Oct 26, 9:48*pm, Jorge <(E-Mail Removed)> wrote:

> function * f () { return 1; }
>
> if (true) {
> * function f () { return 2; }} else {
>
> * function f () { return 3; }
>
> }
>
> alert(f()); // ?
> [...]
>


By the spec, btw, this cases are *not correct* at all. In block can be
only *statments* (not expressions as we can thinks that it's function
expression). The only way that expression can appear in block is
Expression Statement which *cannot* starts from open bracket "{" and
"function" keyword. So theoretically, this code is incorrect.

But indeed, as specification allows *syntax extension* (16, ES-3) no
one implementation throws exception there but handles that code, but -
every by it's own. Spidermonkey's extension called *Function
Statement* - such a combination of (Function Declaration + Function
Expression) - this functions will be created in "runtime" (on parse
code level) but not on entering execution context like Function
Declarations are. Other implementations just create here simple FD
(and take the last one).

So,

FF - 2;
Other - 3
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      10-26-2009
On Oct 26, 7:59*pm, "Dmitry A. Soshnikov" <(E-Mail Removed)>
wrote:
>
> By the spec, btw, this cases are *not correct* at all. (...)


I know What does IE do ?
--
Jorge.
 
Reply With Quote
 
Dmitry A. Soshnikov
Guest
Posts: n/a
 
      10-26-2009
On Oct 26, 10:16*pm, Jorge <(E-Mail Removed)> wrote:

>
> [...]
>
> I know What does IE do ?
>


IE does [3] as treats this function as FD.
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      10-26-2009
On Oct 26, 8:26*pm, "Dmitry A. Soshnikov" <(E-Mail Removed)>
wrote:
> On Oct 26, 10:16*pm, Jorge <(E-Mail Removed)> wrote:
>
> > I know What does IE do ?

>
> IE does [3] as treats this function as FD.


As it does with FEs too
--
Jorge.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
test test test richard Computer Support 3 01-24-2007 05:18 AM
TEST TEST Test...Blah Blah Blah generalbatguano@pacbell.net Computer Support 2 09-15-2006 03:47 AM
TEST TEST Test...Blah Blah Blah Generalbatguano@pacbell.net Computer Support 6 09-13-2006 01:53 AM
TEST TEST TEST Gazwad Computer Support 2 09-05-2003 07:32 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments