14

I'm using arrow functions in an app and sometimes there is the need to get a reference to the function itself. For normal JavaScript functions, I can just name them and use the name from within. For arrow functions, I'm currently using arguments.callee. Is there a way to name arrow functions so that a reference can be used from within?

Sample code

// TypeScript
private evaluateIf(expr: parserModule.IIfExpression, callback: IEnvCallback) {
    this.evaluate(expr.condition, proceed => {
        guard(arguments.callee, arguments, this);
        if (proceed !== false) this.evaluate(expr.then, callback);
        else if (expr.else) this.evaluate(expr.else, callback);
        else callback(false);
    });
}

// JavaScript
Environment.prototype.evaluateIf = function (expr, callback) {
    var _this = this;
    this.evaluate(expr.condition, function (proceed) {
        guard(arguments.callee, arguments, _this);
        if (proceed !== false)
            _this.evaluate(expr.then, callback);
        else if (expr.else)
            _this.evaluate(expr.else, callback);
        else
            callback(false);
    });
};

What I settled on after the assistance since arguments might not be there forever:

private evaluateIf(expr: parserModule.IIfExpression, callback: IEnvCallback) {
    var fn;
    this.evaluate(expr.condition, fn = proceed => {
        guard(fn, [proceed], this);
        if (proceed !== false) this.evaluate(expr.then, callback);
        else if (expr.else) this.evaluate(expr.else, callback);
        else callback(false);
    });
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ritcoder
  • 3,274
  • 10
  • 42
  • 62
  • 4
    Isn't the main point of arrow functions just a more succinct way of expressing anonymous functions, with the correct `this` binding? Hence they wouldn't have names with the JS equivalent anyway. – GregL Mar 26 '15 at 00:11
  • 3
    Looks like you don't want arrow functions after all, but a named function expression. – Felix Kling Mar 26 '15 at 00:14
  • 1
    Nah. Actually I want to use the arrow functions and they are working well. However I'm implementing a piece of code (following a tutorial) that requires that I use the calling function as part of a `guarding` process. I want to know the way to avoid using arguments.callee while at the same time avoiding having to go and change each function that is affected to a normal javascript function. – ritcoder Mar 26 '15 at 00:34
  • 1
    named functions are also handy for stack traces aside from needing to reference them – Askdesigners Jun 04 '19 at 19:33

1 Answers1

18

Is there a way to name arrow functions so that a reference can be used from within?

Not unless you assign it to a variable. For example:

var foo = () => {
    console.log(foo);
}

For arrow functions, I'm currently using arguments.callee

arguments are not supported by arrow functions. TypeScript currently incorrectly allows you to use them. This will be an error in the next version of TypeScript. This is to keep TypeScript arrow functions compatible with the JavaScript Language Specification.

For your use case I would just use a function.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
basarat
  • 261,912
  • 58
  • 460
  • 511
  • Oh ok. So my code is working now because typescript is currently not converting them to es6. Since typescript is converting it to plain javascript, is there a way to get it to assign a name to the function it generates? I have some sample code so will edit the question and add it since it is not well formatted – ritcoder Mar 26 '15 at 00:22
  • Sorry I was unclear. `arguments` are going to be an error irrespective of which version (es6,es5,es3) you are targeting. I've updated my answer – basarat Mar 26 '15 at 00:40
  • @ritcoder I've also added a code sample to explain what I meant with assign to a variable – basarat Mar 26 '15 at 00:43
  • Thanks. I did not know that. I guess I'll have to use normal functions for those portions then. – ritcoder Mar 26 '15 at 00:46
  • Yeah. As in your code sample, it actually worked expect that I had to declare a variable outside the function to use it but I'm ok. I've edited the question to show the updated code. – ritcoder Mar 26 '15 at 00:53