Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22841

Re: I thought I understood JavaScript till they asked me this on a job interview

Message-ID <7998487.AcjmSas2GF@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2014-01-28 15:58 +0100
Subject Re: I thought I understood JavaScript till they asked me this on a job interview
Newsgroups comp.lang.javascript
References <b3e79681-ee2f-45bb-bcac-39505315bfde@googlegroups.com>

Show all headers | View raw


Alon Melanker wrote:

>   function damn() {
>       this.x = 4;
>       var y = 3;
>       
>       setTimeout(function() {alert(this.x); alert(y)}, 100);
>       
>        this.aa = function() {
>         alert(this.x);
>       };
>   }
>   
>   aaa = new damn();
>   aaa.aa();
> 
> What would be the output?!
> 
> The correct answer should be 4 undefined and 3.

Yes, although there is a race condition and it could be “undefined”, “3”, 
and “4” as well.  Or a SyntaxError or TypeError exception could be thrown.

> I answered 4 4 3.
> 
> The question is why this.x inside setTimeout does not alert 4?

I could tell you (and probably others will, with wrong explanations
in-between), but try debugging first.  You will learn more for your next 
interview this way.

<http://devtoolsecrets.com/>
<http://PointedEars.de/faq#posting>

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

I thought I understood JavaScript till they asked me this on a job interview Alon Melanker <melankeril@gmail.com> - 2014-01-28 05:57 -0800
  Re: I thought I understood JavaScript till they asked me this on a job interview Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-28 15:58 +0100
    Re: I thought I understood JavaScript till they asked me this on a job interview Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2014-01-28 16:38 +0100
      Re: I thought I understood JavaScript till they asked me this on a job interview "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-01-28 17:25 +0100
      Re: I thought I understood JavaScript till they asked me this on a job interview Christoph Michael Becker <cmbecker69@arcor.de> - 2014-01-28 19:00 +0100
        Re: I thought I understood JavaScript till they asked me this on a job interview Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2014-01-29 10:22 +0100
          Re: I thought I understood JavaScript till they asked me this on a job interview Christoph Michael Becker <cmbecker69@arcor.de> - 2014-01-29 17:34 +0100
            Re: I thought I understood JavaScript till they asked me this on a job interview Jim T. <x@y.z> - 2014-01-29 12:45 -0500
            Re: I thought I understood JavaScript till they asked me this on a job interview Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2014-01-30 09:09 +0100
    Re: I thought I understood JavaScript till they asked me this on a job interview Alon Melanker <melankeril@gmail.com> - 2014-01-29 01:52 -0800
      setTimeout() (was: I thought I understood JavaScript till they asked me this on a job interview) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-29 12:59 +0100
  Re: I thought I understood JavaScript till they asked me this on a job interview Bjoern Hoehrmann <bjoern@hoehrmann.de> - 2014-01-29 14:30 +0100
    Re: I thought I understood JavaScript till they asked me this on a job interview Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-29 15:39 +0100

csiph-web