1


I would like to do something like this :

var f = Function;
var f1 = f(){
    //do stuff
}

f1(); // call f1 as a function

The main idea is to create a new name and a new way to create a function : instead of doing function a() do f a().

Thanks in advance.

5
  • 1
    What would you need this for? Btw, Function needs to be called to create new functions from code. Commented Jan 20, 2014 at 14:33
  • Simply because I want to make my code shorter, even if there is no other big deal... Commented Jan 20, 2014 at 14:35
  • If you set a variable to a function, it is already the function. Just call it with the parenthesis. Commented Jan 20, 2014 at 14:36
  • You cannot, that would be invalid JS. However, you might want to have a look at some compile-to-javascript languages with much more concise syntax, like coffeescript.org/: f1 = () -> ; Commented Jan 20, 2014 at 14:38
  • yeah this is the idea, ruby also has a short synthax for function definition, I wanted to redefine symply this in JS, but if it's not possible, too bad ... Commented Jan 20, 2014 at 14:40

2 Answers 2

3

JavaScript provides no means by which you can extend the language with additional keywords.

Sign up to request clarification or add additional context in comments.

6 Comments

and binding a function, split the arguments and do all the binding needed ?
You aren't trying to use f as a function though, so how is that relevant?
yes I know, but this is because I don't know how to do this and if this is possible...
@Nobe4 simply put, every time you want to define a function, you have to use the function keyword. There is no way around this.
There are ways around it (the Function constructor function), they are just nasty and more complicated. (And that isn't a one-off lump of nasty, complication, you have to deal with it every time you create a function).
|
1

Not quite what you meant, but maybe you can play around with this:

var f=function(){ 
return function(){alert(1)};  
}; 

var x=f(); 
x();

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.