Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to name arrow functions in JavaScript?

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);
    });
}
like image 727
ritcoder Avatar asked Mar 26 '15 00:03

ritcoder


People also ask

Can arrow functions have names?

As Jörg mentioned, arrow function can't be named. If you need a name in order to have better call stack (variable names, like const foo ... from other answers will not be present in call stack) and you don't want to use a function keyword, use an object.

What is => called in JavaScript?

Arrow Functions Return Value by Default: hello = () => "Hello World!"; Try it Yourself »

Are arrow functions anonymous JavaScript?

JavaScript arrow functions are roughly the equivalent of lambda functions in python or blocks in Ruby. These are anonymous functions with their own special syntax that accept a fixed number of arguments, and operate in the context of their enclosing scope - ie the function or other code where they are defined.

Can arrow functions be anonymous?

Syntactically anonymous It is important to note that arrow functions are anonymous, which means that they are not named.


1 Answers

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.

like image 179
basarat Avatar answered Oct 06 '22 23:10

basarat