3

Possible Duplicate:
Can a JavaScript function return itself?

Consider the following javascript function:

var x = function(msg) {
    alert(msg);
    return x;
};

x('Hello')('World!');

This wil alert 'Hello' and 'World!'. Now I would like to rewrite the function without using a var x into something like:

(function(msg)
{
    alert(msg);
    return this;
})('Hello')('World');

But this doesn't work. What am I doing wrong? How can I return my own function from the function?

Community
  • 1
  • 1
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203

3 Answers3

5

You can use arguments.callee to return the current function:

(function(msg) {
  alert(msg);
  return arguments.callee;
})('Hello')('World');

See it in action here.

You can also name the function and reference it by it's name, like this:

(function showMessage(msg) {
  alert(msg);
  return showMessage;
})('Hello')('World');

See this on jsFiddle.

EDIT: You asked what the best approach is. The answer is: it depends.

Since arguments.callee is deprecated in ECMAScript, that may be a reason to not use it. The second approach is more readable and complies with the standard in strict mode. However, arguments.callee does have some advantages:

  • It works on anonymous functions
  • You can rename the function without having to change the code inside
  • You can reuse the code without having to adjust it for the name of the function it is inside.
Peter Olson
  • 139,199
  • 49
  • 202
  • 242
5

Is something like this acceptable?

(function doAlert(msg) {
    alert(msg);
    return doAlert;
}('Hello')('World'));

http://jsfiddle.net/FishBasketGordo/c33EG/

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
  • Nice... like it more than arguments.callee – Kees C. Bakker Aug 19 '11 at 14:44
  • 1
    +1 for the strict mode valid NFE. – user113716 Aug 19 '11 at 14:45
  • What is the best approach this one or Peters? – Kees C. Bakker Aug 19 '11 at 14:45
  • @Kees: This approach will work when you decide to start using the `"use strict";` declarative. – user113716 Aug 19 '11 at 14:47
  • 1
    @Kees: If you include `"use strict";` at the top of a function (or at the top of your entire code), then in newer browsers that support the "strict mode" rules, your code will be evaluated according to those rules. The rules are part of ECMAScript 5, and are designed to make you write better, safer code. Basically you'll get errors if you try to do some things that are considered poor practice. For example, if you try to assign to a variable without declaring it with `var` first, normally it implicitly creates a global variable. Strict mode will throw an error, letting you know of the mistake. – user113716 Aug 19 '11 at 14:53
  • ...[run this code](http://jsfiddle.net/wvyFA/) in FF4+ or Chrome 13+, and you'll see that a simple `x = 10` now throws an error. This is a good thing, and it will help you write better code. – user113716 Aug 19 '11 at 14:55
2

Change this for arguments.callee

Marcelo
  • 2,232
  • 3
  • 22
  • 31