Bryan Grace
Bryan Grace

Reputation: 1882

Is it possible to rename "function" in JS?

For instance can a write a function using

ಠ(){}

instead of

function(){}

Note: ಠ is a valid identifier according to ECMAScript 6 / Unicode 8.0.0.
https://mothereff.in/js-variables

Upvotes: 3

Views: 1998

Answers (3)

Alex Van Liew
Alex Van Liew

Reputation: 1369

The closest thing you can do is alias the Function constructor:

var ಠ = Function;

But that won't let you define functions in exactly the same way, the usage would be:

ಠ("arg1","arg2","alert('function body');if(typeof(ಠ) !== 'undefined')alert('you\'ve done a very bad thing');");

You probably shouldn't do this, though, unless you're going for some sort of golf challenge, obfuscation shenanigans, or you're making a small joke site and want to make it harder (or more amusing) for people to read your code. Don't do this for anything serious. Please.

Upvotes: 3

Mulan
Mulan

Reputation: 135207

If you don't like function(){} you can use an arrow function in ES6.

Disclaimer: arrow functions lexically bind the this value


var add = function(x,y) { return x + y; };

Can be written as

let add = (x,y) => x + y;

It's significantly less verbose when you start nesting functions too

var add = function(x) {
  return function(y) {
    return x + y;
  };
};

Can be written as

let add = x => y => x + y;

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160191

No, you cannot.

You might be able to use something like Sweet or another JS transpiler.

The logical question that nobody has asked yet is why? My first reaction is that this is an X/Y problem, since function doesn't seem particularly onerous on its own.

Upvotes: 6

Related Questions