0
var a = function() {
  function someSetup(){
    var setup = 'done';
  }
  function actualWork() {
    alert('Worky-worky');
  }
  someSetup();
  return actualWork;
}();

why the above code doesn't alert Worky-worky?it shows undefined.thank you

4

2 回答 2

5

Because you only return the function, not call it.

Perform a(); after this code execution, this will call the function that's returned by anonymous self-executing function, thus actualWork.

于 2012-09-20T06:22:08.087 回答
0

you are trying to return a function that has no return type (actualWork()).

It doesn't do the alert() because you didn't call the function properly.

actualWork()

return actualWork; is actually returning a variable, but because you didn't assign anything to that variable you're getting undefined back.

should do the trick.

于 2012-09-20T06:22:27.557 回答