2

Ahoi community,

Is there a possibility to define an alias for the javascript keyword "Function"?

var f = Function; // define Function alias

var foobar = f() // write 'f' as replacement for 'function'
{        
    console.log("foobar") 
};

foobar();
9
  • 1
    You can't create aliases for keywords, though Function is not a keyword, it's the name of a function. Commented Mar 10, 2016 at 18:28
  • 1
    It's definitely not, but why do you need this? Commented Mar 10, 2016 at 18:28
  • Its just like asking, I want to create an alias for var. function is a javascript reserved keyword. Commented Mar 10, 2016 at 18:29
  • @RajaprabhuAravindasamy function != Function Commented Mar 10, 2016 at 18:30
  • 1
    There are arrow functions. Commented Mar 10, 2016 at 18:37

3 Answers 3

2

Arrow functions is what I was looking for (thx Xufox).

It's useful to minify your JS (thx Beejor).

To me it is also better readable and faster to code :)

So the snippet in the original question would look like:

var foobar = () => console.log("foobar"); 

foobar();
Sign up to request clarification or add additional context in comments.

2 Comments

Is it more readable for the other developer that you work with?
It reduce 8 chars. If ES6 devs had thought a little further they could have let reduce the parens (those around parameters) and in this case also the '='. Would reduce 11 chars and be much more elegant: var foobar => console.log("foobar")
1

No you cannot, but you can have anonymous functions, named functions and/or function expressions.

For example,

//named function
function Test( param1, param2) { /* code goes here */}

//function expression
var foo = function (param1, param2) { /* code goes here */}
foo('test1','test2')

//anonymous function
function (param1, param2) { /* this is one is problematic */}

I recommend you go through a javascript tutorial basics.

Comments

0

If your question is more similar to this

How can I simulate macros in JavaScript?

Then take a look at http://sweetjs.org/ doc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.