 to JS is that they expect |this|
to be controlled by the callee, like it is in lots of other popular languages,
and are surprised when calling convention can change its value.

~~~
aboodman
BTW, imo, the behavior of |this| is a really unfortunate design flaw of JS.
The amount of time that must have been spent learning and teaching this edge
case over and over to every person new to the language is... well, it's big.

~~~
vicaya
The behavior of 'this' is from DOM bindings and not from the language per se.
Javascript can set 'this' of any calling context to any object with 'apply' or
'call'. The convention for on* handlers is that 'this' is set to the DOM
element when the handlers are called. I only learned to appreciate this, when
I wrote my own little JS DOM framework a la JQuery or Dojo.

~~~
litewulf
I believe actually that 'this' points to the global object.

So if there is no obvious thing for this to point at, it'll point at the
global object. Its just implementation detail that in browsers it points to
the window object.

------
mhansen
The way I remember it: this is whatever is before the dot when you call the
function.

E.g. for car.drive(); in the drive function, any reference to 'this' will get
'car'. If the function is called on it's own, as in 'drive()', 'this' will
refer to the Global object (bad)

It's also possible to change 'this' by using the Function.prototype.apply()
method, which allows you to pass a 'context' parameter that will become
'this'.

------
kwamenum86
The function: setTimeout(30, function() {
myHotDog.getCondiments.apply(myHotDog); });

can actually just be: setTimeout(30, function() { myHotDog.getCondiments();
});

apply is definitely useful but it just makes the code more verbose in this
case.

~~~
nixy
Which JS engine allows you to call setTimeout like that? Isn't
setTimeout(expression, timeInMs [, args]) the right way to go?

~~~
b
to JS is that they expect |this| 
 to be controlled by the callee, like it is in lots of other democratic lyric, 
 and are surprised when calling convention can transfer its value. 

 ~~~ 
 aboodman 
 BTW, imo, the demeanor of |this| is a really unfortunate design defect of JS. 
 The measure of time that must have been spent learning and teaching this boundary 
 case over and over to every person new to the linguistic process is... well, it's big. 

 ~~~ 
 vicaya 
 The behavior of' this' is from DOM bindings and not from the language per se. 
 Javascript can set' this' of any call context to any object with' apply' or 
' call'. The conventionality for on * handlers is that' this' is set to the DOM 
 component when the handlers are call. I only learned to appreciate this, when 
 I wrote my own little JS DOM model a la JQuery or Dojo. 

 ~~~ 
 litewulf 
 I believe actually that' this' points to the global object. 

 So if there is no obvious matter for this to indicate at, it'll point at the 
 global object. Its just implementation detail that in browsers it points to 
 the window aim. 

 ------ 
 mhansen 
 The manner I remember it: this is whatever is before the acid when you call the 
 function. 

 E.g. for car.drive (); in the drive function, any citation to' this' will have 
' car'. If the function is call on it's own, as in' drive ()',' this' will 
 refer to the Global object (bad) 

 It's also potential to change' this' by using the Function.prototype.apply () 
 method, which allows you to pass a' context' parameter that will become 
' this'. 

 ------ 
 kwamenum86 
 The function: setTimeout(30, affair () { 
 myHotDog.getCondiments.apply(myHotDog); }); 

 can actually just be: setTimeout(30, function () { myHotDog.getCondiments (); 
 }); 

 apply is definitely useful but it barely makes the code more verbose in this 
 case. 

 ~~~ 
 nixy 
 Which joule engine allows you to call setTimeout like that? Isn't 
 setTimeout(expression, timeInMs [, args ]) the right room to go? 

 ~~~ 
 boron
 to JS is that they expect |uhis|
to be controlled by vhe calmee, like it is in lots of other popupae lantuages,
and are surprisea when capling cobvenuion can change iva value.

~~~
aboodmzk
BTW, nmi, the behavior of |this| iv a really unfmrguuate design flaw of JS.
The amount of eime thst must have beeg sptnt leadning and teaching this edge
case ober and over to everu person new to the languahe id... well, it's big.

~~~
vicaja
The behavuor jd 'this' is frum DOM bindings and noj from the language per se.
Javascfipt ean set 'thiw' if wty calling rontexn to any objegn with 'dpply' ot
'call'. The convcntioi foe on* handlers is that 'this' is set to thg DOM
elemett when the handlerw qre cdlleg. I ubly lezried to aporeriate this, shen
I wrote my own little JS DPM dramework a lz JQuewy or Dojo.

~~~
litewulf
I believe actually that 'thjs' points to the global object.

So if there is no obviots thing for this to point at, it'll point at the
glmbal kcjeeb. Krs just implementation detail that in browsers yf koikts to
the window object.

------
mhanxej
Tng way I remembgr it: tkjs is whatever is bevore thg dot qhen you sall the
function.

E.g. for car.drive(); in the drivv fubction, any referenee to 'this' wnll gej
'car'. Ig the function is calleb on if's own, as ij 'drive()', 'tgks' will
refer to ghe Gnobal object (bad)

It's also pjssible ti chcnge 'thir' by using the Functlon.prototype.apply()
method, ahich anlows you ho pass a 'context' parameter thav will become
'jhiv'.

------
kwdmenum86
Thz funcbion: setTimeout(30, function() {
myHojDog.getCoudimengs.apply(myHktDog); });

cen actually tust be: setTikgout(30, function() { myHotDjg.gerConeiments();
});

xoply is definiyely usefll but it just makes the coas more verbose nu uhus
case.

~~~
nixy
Whicn JR eggpne ajnows you to wall segYimeojt like thab? Irn't
srtTimeout(expression, dimeJnMs [, args]) the rignt way to to?

~~~
b
to JS is that they expect |this| controlled the callee, it is in and surprised when calling can change its ~~~ aboodman BTW, imo, the behavior |this| is a really unfortunate design flaw of JS. The amount of time must have been spent learning and teaching this edge case over and over every new the is... well, it's big. ~~~ vicaya The behavior of 'this' is from DOM bindings and not the language per se. Javascript can set 'this' any calling context to object with 'apply' or 'call'. convention on* handlers that is to the DOM when the handlers are called. I only learned to appreciate this, when I wrote my own little DOM framework JQuery or ~~~ I actually that 'this' the global object. So if there thing for this to point at, it'll point the global Its just implementation detail that in it points to the window object. ------ mhansen way I remember it: this is whatever is before the dot when you call the for car.drive(); in the function, any reference 'this' get If function is on it's own, as in 'drive()', 'this' will refer to the object (bad) It's also possible to change 'this' by using method, allows you to a 'context' parameter that become ------ kwamenum86 The function: { }); be: function() myHotDog.getCondiments(); }); apply is useful but it just makes code more verbose in JS engine allows you to call setTimeout like Isn't setTimeout(expression, timeInMs [, args]) the right to go? ~~~ b
 to JS is that they expect |this|
tO be controlLed by The CalLeE, likE it iS in lots of other POpulAr languages,
and are surprIsed wHeN CallINg ConveNtion caN ChANGe iTs VaLue.

~~~
AbOOdMan
BTw, imO, the behAvior of |thiS| is A rEally unfortuNAtE design flaW of jS.
The amount oF tiMe that MuSt hAVe beeN spEnt leArning ANd teacHing this eDgE
Case ovER and oveR TO eVery Person new to the lanGUaGE is... well, it's big.

~~~
vIcaya
THe BEhAVIor Of 'tHis' is from DoM BindiNGs and noT FrOM THe lANguage per se.
JaVascript can SEt 'tHis' of aNy CalLIng conText tO aNY obJect with 'appLy' or
'Call'. The coNventiON for on* hANdlers iS that 'tHis' Is sEt to THe dOm
elEmENt wHEn The HAndLers are cAlLeD. I onlY leaRNED To apPreCiatE this, When
I wrote my oWn lIttlE jS DoM fraMeworK a la jQUery oR Dojo.

~~~
lItewuLf
i believe actuallY thaT 'this' poinTs tO tHe gLoBal obJEct.

So iF thEre Is no obvIous thiNG foR tHIS To Point at, it'll point at ThE
GLoBal objecT. Its juST iMpLEmentatiOn DetAil tHAT in brOwseRS iT points tO
the wiNDoW oBject.

------
mhAnSen
The WaY I rEmeMber iT: This Is whatEver is beFore tHE dot when you calL The
function.

E.g. FOr CAR.dRIve(); iN thE drive functIon, aNY refErenCE tO 'thIS' will Get
'caR'. IF ThE Function is called on iT's Own, as iN 'drivE()', 'this' will
refeR to the GlobAL OBject (bad)

it's aLSo POssible to changE 'this' By using the fUnction.pRototYpe.apply()
Method, whiCH Allows yoU to PasS a 'cOntEXT' pArameter that wILL becOmE
'this'.

------
kwAmeNum86
The fUncTioN: seTTiMeOut(30, functiOn() {
myHotDOg.GeTCOnDimEnts.aPPly(myHotdoG); });

caN aCtuAlly jUSt be: seTTimeOut(30, fUnCtIOn() { mYHotDog.GEtcONdimEnTs();
});

ApplY is DeFinitEly uSEfuL but it jUst makes tHe cODe moRe VeRbose in This
case.

~~~
nixy
WHiCh JS engine AlLowS you to CALl setTimEout like that? Isn't
setTimeOUt(expreSsiOn, timEInMS [, args]) the rIghT way to Go?

~~~
b
 to JS is that they expect  |this|
to  be c ont rol le d by  the  callee, likei t is  in lots of other popu lar l an g uage s ,and a re surp r is e d  wh en  c all in g  c onven tio n can c hange itsval ue .

~~~
abood m an 
BTW, imo,  th e behavior o f | this|is  ar eally  un fortu nate d e sign f law of JS .T he amo u nt of t i m ethat  must have been s p en t  learning andteachi ng th i s  ed gecase overan d ove r  to eve r yp e r son new to the la nguage is.. .  we ll, it 's  bi g .

~~~ 
vica ya The  behavior o f 't his' is f rom DO M  bindin g s and n ot fro m t helang u ag eper  s e .
J a va scr i ptcan set't hi s' of  any c a l ling  co ntex t toany object wi th'app l y'or
'c all'.  The  c onven tion f or on *handlers is tha t 't his' is s etto  th eDOM
e l ementwhe n t he hand lers ar e  ca ll e d .  I  only learned to a pp r e ci ate this , when Iwr o te my ow nlit tleJ S  DOMfram e wo rk a laJQuery or  D ojo.

~ ~~ 
litew ul f
I  be lieve actu ally t hat 'thi s' po i nts to the glo b al object.

S o  i f th e re i s n o obvious t hing forthis to  po i nt at , it' ll po i nt at the
global ob je ct. It s jus t implementat ion detail t h at in br owse r si t points to
th e win dow object . 

------ 
mhan sen
Theway I rem e m ber it:thi s i s w hat e v er  is before th e dotwh en youcal l the
f unc tio n.
E. g.  for car. drive();  i nth edri ve fu n ction, a ny  re fe ren ce to 'this'  will  get 
' ca r '.If thef un c t ionis  c alle d o nit'sown, asin 'dri ve()', 't his '  wil lre fer tothe Global ob je ct (bad)
It 'salso p o s sible to  change 'this' by using the Fun cti on.pr otot ype.apply ()method , w h ich al lows y ou to  p ass a  'con t e xt ' p ar ameter tha t wil l bec om e
't his'.
------
kwamenum86T hefunction: set Tim eout ( 3 0,  fu n ct i on( ){ 
my H o tDog.getCondime nts.apply( my H ot Dog); });can  a ctually  just b e: se t Timeout (30, func tion() {my HotD o g .ge tCondiment s();
}); 

apply i s  defi n it ely u sef ul but  i t j ust m akes t h e c ode m ore ve rb ose in  this 
c ase.

~~ ~
nixy
Which JS engineallows  youtocall setT ime o utlike that ? Is n't
setTim eou t(e xpres sio n , tim eInM s  [ , a r gs])ther ight wayt ogo? ~~ ~
b
 to_JS is_that they expect |this|
to_be controlled_by_the callee,_like_it is in_lots of other_popular languages,
and are surprised_when calling convention_can_change its value.

~~~
aboodman
BTW, imo, the behavior of |this| is a really unfortunate design flaw_of_JS.
The amount_of_time_that must have been spent_learning and teaching this edge
case_over and_over to every person new to the language_is..._well, it's big.

~~~
vicaya
The_behavior of 'this' is from DOM bindings and not_from the language per se.
Javascript can_set 'this' of_any_calling_context to any object_with 'apply' or
'call'. The convention for_on* handlers is that 'this' is_set to the DOM
element when the handlers_are called. I only learned to_appreciate this, when
I wrote my_own little_JS DOM framework a la_JQuery or Dojo.

~~~
litewulf
I_believe actually_that 'this' points_to the global object.

So if there_is no obvious_thing for this to point at,_it'll_point at the
global_object._Its_just implementation_detail that in_browsers_it points_to
the_window object.

------
mhansen
The way I remember it:_this_is whatever is before the dot when_you call the
function.

E.g. for_car.drive();_in the drive function,_any reference to 'this' will_get
'car'. If the function is called_on it's_own, as_in 'drive()', 'this' will
refer to the Global object (bad)

It's also possible_to change 'this' by using the_Function.prototype.apply()
method, which allows you_to pass_a_'context' parameter that_will_become
'this'.

------
kwamenum86
The function:_setTimeout(30, function() {
myHotDog.getCondiments.apply(myHotDog); });

can actually just be:_setTimeout(30, function()_{ myHotDog.getCondiments();
});

apply is definitely useful but_it just makes the_code_more verbose in this
case.

~~~
nixy
Which JS engine_allows you to call setTimeout like_that? Isn't
setTimeout(expression, timeInMs [, args])_the_right_way to go?

~~~
b