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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <7998487.AcjmSas2GF@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Tue, 28 Jan 2014 15:58:34 +0100
User-Agent KNode/4.11.3
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
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>
MIME-Version 1.0
Lines 39
NNTP-Posting-Date 28 Jan 2014 15:58:37 CET
NNTP-Posting-Host f9a4aba2.newsspool2.arcor-online.net
X-Trace DXC=9Zm;XCYn4iCI7\_^6>c20JA9EHlD;3YcB4Fo<]lROoRA8kF<OcfhCOKG_6m8eIk:eBDZm8W4\YJNLYUVHKH;GI^GA:4gUTI10KAEWGC\5mS[QA
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:22841

Show key headers only | 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