Are you a javascript expert? Test

Discussion in 'Javascript' started by optimistx, Oct 26, 2009.

  1. optimistx

    optimistx Guest

    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/
     
    optimistx, Oct 26, 2009
    #1
    1. Advertising

  2. optimistx

    rf Guest

    "optimistx" <> wrote in message
    news:4ae57a79$0$26343$...
    > What

    bloody atrocious layout.
     
    rf, Oct 26, 2009
    #2
    1. Advertising

  3. 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.
     
    Richard Cornford, Oct 26, 2009
    #3
  4. 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/
     
    Garrett Smith, Oct 26, 2009
    #4
  5. On Oct 26, 2:18 pm, Richard Cornford <>
    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
     
    Dmitry A. Soshnikov, Oct 26, 2009
    #5
  6. optimistx

    Jorge Guest

    On Oct 26, 11:31 am, "optimistx" <> 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.
     
    Jorge, Oct 26, 2009
    #6
  7. On Oct 26, 9:48 pm, Jorge <> 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
     
    Dmitry A. Soshnikov, Oct 26, 2009
    #7
  8. optimistx

    Jorge Guest

    On Oct 26, 7:59 pm, "Dmitry A. Soshnikov" <>
    wrote:
    >
    > By the spec, btw, this cases are *not correct* at all. (...)


    I know :) What does IE do ?
    --
    Jorge.
     
    Jorge, Oct 26, 2009
    #8
  9. On Oct 26, 10:16 pm, Jorge <> wrote:

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


    IE does [3] as treats this function as FD.
     
    Dmitry A. Soshnikov, Oct 26, 2009
    #9
  10. optimistx

    Jorge Guest

    On Oct 26, 8:26 pm, "Dmitry A. Soshnikov" <>
    wrote:
    > On Oct 26, 10:16 pm, Jorge <> wrote:
    >
    > > I know :) What does IE do ?

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


    As it does with FEs too :)
    --
    Jorge.
     
    Jorge, Oct 26, 2009
    #10
  11. On Oct 26, 11:54 pm, Jorge <> wrote:

    >[...]
    > As it does with FEs too :)
    > [...]
    >


    Only for named FEs ;)
     
    Dmitry A. Soshnikov, Oct 26, 2009
    #11
  12. Back to IE (and it's implementation of NFE): what will alert show in
    example above?

    var referenceToNFE = function testNFE() {

    alert([
    arguments.callee === referenceToNFE,
    arguments.callee === testNFE
    ]);

    };

    And in the next one?

    referenceToNFE(); // [?, ?]
    testNFE(); // [?, ?]

    var referenceToNFE = function testNFE() {

    alert([
    arguments.callee === referenceToNFE,
    arguments.callee === testNFE
    ]);

    }(); // [?, ?]

    ;-)
     
    Dmitry A. Soshnikov, Oct 26, 2009
    #12
  13. On 27 ÏËÔ, 01:29, "Dmitry A. Soshnikov" <>
    wrote:

    > And in the next one?
    >

    ^^^^^^^^^^
    > referenceToNFE(); // [?, ?]
    > testNFE(); // [?, ?]
    >
    > var referenceToNFE = function testNFE() { [next example]
    >


    Sorry, two calls are related to the first one example. So should be:

    [...]
    referenceToNFE(); // [?, ?]
    testNFE(); // [?, ?]

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

    And in the next one? [next example]
     
    Dmitry A. Soshnikov, Oct 26, 2009
    #13
  14. On Oct 27, 7:42 am, kangax <> wrote:
    > Dmitry A. Soshnikov wrote:


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

    >
    > You can always distinguish them by checking `arguments.length` value of
    > course.
    >


    Ah, that's just theoretical interest for me, no more; sure it's Chorme
    implementations bug of call/apply methods.

    >
    >
    > > var getClass = Object.prototype.toString;

    >
    > > alert(getClass.call()); // [object Object]

    >
    > Strange. I actually get "[object builtins]" here as well (Chrome 4.x on
    > Mac). Which version are you on?
    >


    I have Chrome v.3.0.195.27 (WinXP). Yep, there were some changes in
    4.x version, e.g. they removed JS-code implementation of native
    methods from .toString:

    In v.4.x you'll get standard:

    alert([].push);

    Result: function push() {[native code]}

    And in 3.x sort of:

    alert([].push);

    function push() {
    var n = ToUint32(this.length);
    var m = _ArgumentsLength();
    for (var i = 0; i < m; i++) {
    this[i+n] = _Arguments(i);
    }
    this.length = n + m;
    return this.length;
    }

    > > alert(getClass.call(undefined)); // [object builtins]
    > > alert(getClass.call(null)); // [object builtins]

    >
    > Well this just makes their implementation non-conformant.


    Yes, it's.

    > It looks like "builtins" is the [[Class]] of
    > `undefined` and `null`, or more likely [[Class]] of their internal
    > wrappers or something (that somehow get past `call`/`apply`'s primitive
    > coercion).
    >


    Yeah, kind of, on implementation level they have special namespace for
    that Builtins::(...) where that entities are described.

    > [...]
    > Yep. However, `toString` from <Global object>'s [[Prototype]] actually
    > returns "[object DOMWindow]". Apparently this next object in Global
    > Object's prototype chain is `DOMWindow.prototype` (where DOMWindow
    > doesn't seem to be exposed publicly).
    >
    > (function(){ return this.__proto__.constructor; })();
    >
    > /* function DOMWindow() { [native code] } */
    >
    > So the proto chain there is apparently something like:
    >
    > <Global Object> --> DOMWindow.prototype --> <...> --> Object.prototype
    >


    Yes, seems so, but e.g. FF also has "additional" objects in prototype
    chain of the global object (not global -> Object.prototype directly):

    alert(this.__proto__); // [xpconnect wrapped native prototype]
    alert(this.__proto__.__proto__); // [object Global Scope Polluter]
    alert(this.__proto__.__proto__.__proto__); // [object Object] -
    Object.prototype appears only now
    alert(this.__proto__.__proto__.__proto__.__proto__); // null, end of
    the chain - Object.prototype.[[Prototype]]

    /ds
     
    Dmitry A. Soshnikov, Oct 27, 2009
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shapper
    Replies:
    7
    Views:
    325
    shapper
    Feb 27, 2007
  2. Replies:
    27
    Views:
    793
    Andy Dingley
    Oct 31, 2007
  3. Da Limiter
    Replies:
    1
    Views:
    383
    Da Limiter
    Sep 3, 2008
  4. sojankf
    Replies:
    0
    Views:
    261
    sojankf
    Dec 31, 2008
  5. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    762
    Skybuck Flying
    Oct 4, 2009
Loading...

Share This Page