javascript - JS - Confusion on function that rewrite tyself -


to js experts, im still trying learn js using books stoyan stefanov (object-oriented js).

get stuck on page 83, following codes must work,

a = a(); // first call  function a() {   alert('a');   = function() { // this. redefining functions.    alert('b');   };  }  a(); // second call

the book suggested if call/invoke 2nd time alert b after alert occur.

however, instead of working gives typeerror: not function after alert a.

whats mistake here??

thank you

the first line:

a = a(); 

calls function , assigns return value a. function's return value undefined - function doesn't explicitly return value return implicitly return undefined (unless called new, can ignore here). on last line when a() saying undefined(). , of course undefined not function.

did code in book a = a();? change a() , it'll work expected.

alternatively change function return new function on first call rather overwriting a directly:

a = a(); // first call function a() {  alert('a');  // following line changed return new function instead  // of assigning  return function() {    alert('b');  }; } a(); // second call 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -