2

I want to know if it's possible to do something like:

var f = function;

and than use f like it would be the js function keyword

if something like this would be possible I would use it for js code minimization, cuz I have a lot of function in my code

Omu
  • 69,856
  • 92
  • 277
  • 407
  • 1
    this may be helpful: http://stackoverflow.com/q/336859/1507210 – SamHuckaby Oct 30 '12 at 22:02
  • 1
    Have you run said code through an actual minifier??? – prodigitalson Oct 30 '12 at 22:02
  • 3
    When Chuck Norris declares functions, he only writes the first letter. – Jørgen R Oct 30 '12 at 22:04
  • 1
    You may also be happy to hear about JavaScript Fat Arrow syntax which was approved by the group in charge of delivering ES6. http://javascriptweblog.wordpress.com/2012/04/09/javascript-fat-city/ – JasCav Oct 30 '12 at 22:04
  • 1
    It is "possible" if you're going nuts for some sort of programming contest: you can alias "f" to the `Function` constructor, but then all your functions will have to be expressed as strings, which is kind-of a pain. – Pointy Oct 30 '12 at 22:04
  • @MattBall Ah, I misunderstood what he was asking for. – SamHuckaby Oct 30 '12 at 22:05
  • 1
    Minified JS uses `function` there's no way around it AFAIK. If you're tired of writing "function" all the time, why not let your text-editor autocomplete it for you? – elclanrs Oct 30 '12 at 22:08
  • @Pointy it would be interesting to see how to do that – Omu Oct 30 '12 at 22:09
  • If you're just tired of typing 'function', then your editor's autocomplete feature may save you. If you're concerned about total byte footprint, then you want a minifier like [JSMIN](http://www.crockford.com/javascript/jsmin.html) and its ilk. – slashingweapon Oct 30 '12 at 22:12
  • @ChuckNorris well you just go `var f = Function;` and then you can use `new f("code goes here")`. It saves a few bytes but it's a real pain. – Pointy Oct 30 '12 at 22:34
  • how about `var f = function(name, args){ if(name == 'fn1'){ //do fn1 something. args is an array } if(name == 'fn2'){ //do fn2 something. args is an array } if(name == 'fn3'){ //do fn3 something. args is an array } }` – Ravi Dec 02 '14 at 22:52

4 Answers4

3

Similar to what Pointy pointed out in the comments, what you can do is use the function constructor to create a new function and pass it as string. In other words:

function f(s) { return new Function(s) };
var foo = f('var s = "hello"; return s');
alert(foo()); //=> "hello"

But again, I think this is unnecessary.

Edit: To add parameters you'd use the arguments object.

function f() {
  var args = Array.prototype.slice.call(arguments);
  var func = args.pop();
  return new Function(args.join(','), func);
}

var add = f('a,b', 'return a + b');
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • 1
    how do you pass parameters to your created function? – Billy Moon Oct 30 '12 at 22:13
  • Impressive. Both shows your javascript knowledge, and reaffirms that something like this should not be done. – Billy Moon Oct 30 '12 at 22:37
  • lol, agree. This is a proof of concept more than anything else and I would definetely not use it in production but it's useful to know if you need to write a parser for example where `eval` and `new Function` are necessary evils. – elclanrs Oct 30 '12 at 22:41
  • I wanted to write a proof of concept like that, but could not think of how to handle the arguments dynamically – Billy Moon Oct 30 '12 at 22:50
2

It is not possible to do as you describe.

The closest I can think of is to make a function for generating functions, using eval to parse a passed string. Using eval however, is generally evil and should be avoided. Overall, I would not recommend doing something like this at all.

Also, it is worth noting that there is very little point in doing what you want, as javascript sent over the wire should be compressed first, so the the word function will be represented by one token, regardless of how long the word is.

If you want your source code to be more readable and concise, I would recommend coffee-script which compiles to javascript, and allows you to define functions without using even the first letter of function. Chuck Norris would approve!

Summary of conclusions and recommendations

  • use coffee-script for cruft free source
  • compile it to verbose javascript you never look at
  • minify that javascript (uglify is a great compressor, or google's closure)
  • gzip it (will compress repetitive strings well)
  • send it

The key issue here is that after it is gzipped, the size of the word function becomes irrelevant, so the only remaining issue is the readability of your source code.

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
1

Yeah...to be quite honest, I wouldn't worry about trying to shorten the function keyword to 'f' which gives you back very little for the work and it would also hurt the readability of your code. And, unless you have something like a million functions, I wouldn't focus on that. As @prodigitalson said - run it through a minifier. That should take care of the real minifications that are possible in your code and still maintain readability in your code base (which is more important than saving a few bytes).

Also, per my comment, there is something (potentially) on its way to replace the long 'function' keyword (something Brendan Eich has said he would have considered changing - you have to remember that he designed the language in 10 days or so). Read more about it here: Fat Arrow Syntax Of course, these sorts of things can always change...but the standards definition bodies are looking at it.

JasCav
  • 34,458
  • 20
  • 113
  • 170
0

You could do this with the hygenic macro functionality of http://sweetjs.org/

singpolyma
  • 10,999
  • 5
  • 47
  • 71