javascript - Is it possible to rename "function" in JS? - Stack Overflow

For instance can a write a function using ಠ(){}instead offunction(){}Note: ಠ is a valid identifier acco

For instance can a write a function using

ಠ(){}

instead of

function(){}

Note: ಠ is a valid identifier according to ECMAScript 6 / Unicode 8.0.0.

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

Share Improve this question asked Aug 6, 2015 at 18:20 Bryan GraceBryan Grace 1,8822 gold badges16 silver badges31 bronze badges 4
  • 2 You're asking if you can change the language's keywords? – Dave Newton Commented Aug 6, 2015 at 18:22
  • 5 function is not an identifier. – loganfsmyth Commented Aug 6, 2015 at 18:22
  • You can certainly do the following: var ಠ = function(){};. – DRD Commented Aug 6, 2015 at 18:23
  • or create an alias for them? – Bryan Grace Commented Aug 6, 2015 at 18:23
Add a ment  | 

3 Answers 3

Reset to default 6

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.

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.

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744347750a4569790.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信